How to convert HTML special characters to the real characters with a Java script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stefan Mueller

    How to convert HTML special characters to the real characters with a Java script

    I read data (e.g. äöüÄÖÜçéàè"') from my MySQL database which I'd like to
    show in an input box.

    <?php
    $mysql_data = "äöüÄÖÜçéàè\"'" ;
    $html_data = addslashes(html entities($mysql _data, ENT_QUOTES));

    echo "<script type = 'text/javascript'>";
    echo "function set_old_data() {";
    echo "my_form.input1 .value = var_old_data;";
    echo "}";
    echo "var_old_da ta = '" . $html_data . "';";
    echo "</script>";

    echo "<body>";
    echo "<form name = 'my_form' action = '' method = 'post' accept-charset
    = 'iso-8859-1'>";
    echo "<input type = 'text' name = 'input1' value = '" . $html_data .
    "'>";
    echo "<input type = 'button' value = 'Old Data' onClick =
    'set_old_data() '>";
    echo "</form>";
    echo "</body>";
    ?>

    The command
    echo "<input type = 'text' name = 'input1' value = '" . $html_data . "'>";
    shows my data äöüÄÖÜçéàè"' in the input box perfect.

    But if I click on the button 'Old Data' the Java script function
    'set_old_data' shows in the input box
    &auml;&ouml;&uu ml;&Auml;&Ouml; &Uuml;&ccedil;& eacute;&agrave; &egrave;&quot;& #039;
    instead of
    äöüÄÖÜçéàè"'

    Therefore I need a Java script function with translates
    &auml;&ouml;&uu ml;&Auml;&Ouml; &Uuml;&ccedil;& eacute;&agrave; &egrave;&quot;& #039;
    to
    äöüÄÖÜçéàè"'

    In PHP I could do that with the function
    html_entity_dec ode()

    But how can I do it with a Java script?
    Stefan

    PS: html_entity_dec ode() is the opposite of htmlentities(). It converts all
    HTML entities to their applicable characters from string.


  • Jerry Stuckle

    #2
    Re: How to convert HTML special characters to the real characterswith a Java script

    Stefan Mueller wrote:[color=blue]
    > I read data (e.g. äöüÄÖÜçéàè"') from my MySQL database which I'd like to
    > show in an input box.
    >
    > <?php
    > $mysql_data = "äöüÄÖÜçéàè\"'" ;
    > $html_data = addslashes(html entities($mysql _data, ENT_QUOTES));
    >
    > echo "<script type = 'text/javascript'>";
    > echo "function set_old_data() {";
    > echo "my_form.input1 .value = var_old_data;";
    > echo "}";
    > echo "var_old_da ta = '" . $html_data . "';";
    > echo "</script>";
    >
    > echo "<body>";
    > echo "<form name = 'my_form' action = '' method = 'post' accept-charset
    > = 'iso-8859-1'>";
    > echo "<input type = 'text' name = 'input1' value = '" . $html_data .
    > "'>";
    > echo "<input type = 'button' value = 'Old Data' onClick =
    > 'set_old_data() '>";
    > echo "</form>";
    > echo "</body>";
    > ?>
    >
    > The command
    > echo "<input type = 'text' name = 'input1' value = '" . $html_data . "'>";
    > shows my data äöüÄÖÜçéàè"' in the input box perfect.
    >
    > But if I click on the button 'Old Data' the Java script function
    > 'set_old_data' shows in the input box
    > &auml;&ouml;&uu ml;&Auml;&Ouml; &Uuml;&ccedil;& eacute;&agrave; &egrave;&quot;& #039;
    > instead of
    > äöüÄÖÜçéàè"'
    >
    > Therefore I need a Java script function with translates
    > &auml;&ouml;&uu ml;&Auml;&Ouml; &Uuml;&ccedil;& eacute;&agrave; &egrave;&quot;& #039;
    > to
    > äöüÄÖÜçéàè"'
    >
    > In PHP I could do that with the function
    > html_entity_dec ode()
    >
    > But how can I do it with a Java script?
    > Stefan
    >
    > PS: html_entity_dec ode() is the opposite of htmlentities(). It converts all
    > HTML entities to their applicable characters from string.
    >
    >[/color]

    Uh, maybe ask in a Javascript newsgroup?

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Chung Leong

      #3
      Re: How to convert HTML special characters to the real characters with a Java script

      Stefan Mueller wrote:[color=blue]
      > Therefore I need a Java script function with translates
      > &auml;&ouml;&uu ml;&Auml;&Ouml; &Uuml;&ccedil;& eacute;&agrave; &egrave;&quo t;'
      > to
      > äöüÄÖÜçéàè"'
      >
      > In PHP I could do that with the function
      > html_entity_dec ode()
      >
      > But how can I do it with a Java script?
      > Stefan
      >
      > PS: html_entity_dec ode() is the opposite of htmlentities(). It converts all
      > HTML entities to their applicable characters from string.[/color]

      This relies on IE extensions but you can it's possible to make it
      standard compatible.

      function html_entity_dec ode(s) {
      var span = document.create Element('SPAN') ;
      span.innerHTML = s;
      return span.innerText;
      }

      Comment

      • Chung Leong

        #4
        Re: How to convert HTML special characters to the real characters with a Java script

        On a second thought, it's probably easier if you just dump the text
        into a hidden field.

        Comment

        • Stefan Mueller

          #5
          Re: How to convert HTML special characters to the real characters with a Java script

          > function html_entity_dec ode(s) {[color=blue]
          > var span = document.create Element('SPAN') ;
          > span.innerHTML = s;
          > return span.innerText;
          > }[/color]

          Yea, this is exactly what I'm looking for. But like you mentioned it only
          works for IE and not for Mozilla, Opera, Safari, ...

          Stefan



          Comment

          • Chung Leong

            #6
            Re: How to convert HTML special characters to the real characters with a Java script


            Stefan Mueller wrote:[color=blue][color=green]
            > > function html_entity_dec ode(s) {
            > > var span = document.create Element('SPAN') ;
            > > span.innerHTML = s;
            > > return span.innerText;
            > > }[/color]
            >
            > Yea, this is exactly what I'm looking for. But like you mentioned it only
            > works for IE and not for Mozilla, Opera, Safari, ...
            >
            > Stefan[/color]

            Most browsers support innerHTML, even though it's non-standard, because
            it's so convinent. innerText is supported only by IE as far as I know.
            In the other browsers, you can get the plain text from
            span.firstChild .nodeValue.

            As I said in the follow-up, it's easier to store the original value in
            a hidden field parallel to the input field. Or better yet, use a onload
            handler to capture the initial values of every fields. Something like:

            function saveOriginalVal ues() {
            var inputs = document.getEle mentsByTagName( 'INPUT');
            for(var i = 0; i < inputs.length; i++) {
            var input = inputs[i];
            if(input.type == 'text') {
            originalValues[input.name] = input.value;
            }
            }
            }

            That gives you something that you can reuse across multiple forms.

            Comment

            • Stefan Mueller

              #7
              Re: How to convert HTML special characters to the real characters with a Java script

              > As I said in the follow-up, it's easier to store the original value in[color=blue]
              > a hidden field parallel to the input field. Or better yet, use a onload
              > handler to capture the initial values of every fields. Something like:[/color]

              First of all thanks a lot for your solutions.

              Yea, I've also thought about your solution (hidden fields). However, I've a
              table with hundreds of fields. Today, to sort this table takes already some
              time. But if I add for each field a hidden field the sorting will take to
              much time. Therefore I try to find a solution to get the real characters (ä,
              ö, ü, ...) from the HTML characters (&auml;&ouml;&u uml;, ...).
              In the worst case I've to write my own function (search for '&auml;' and
              replace it with 'ä', then seach for '&ouml;' and replace it with 'ö', ...)

              Stefan


              Comment

              Working...