Encode And Decode entirety of Text or Html to '%xx' format

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

    Encode And Decode entirety of Text or Html to '%xx' format

    How would I modify this form
    to encode *all* the characters
    in the 'source' textarea to the
    '%xx' format & place result
    code into the 'output' textarea?
    (cross browser compatable)

    Any help is appreciated.

    Regards.

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Encode And Decode Entire Text or Html to '%xx' format
    </title>
    </head>
    <body>
    Text and Html "%xx" Converter<br>
    <form name="form1" method="post" action="">
    Original Text:<br>
    <textarea name="source" cols="79" rows="8" wrap="VIRTUAL"> Original Text or Html code
    to have the *entirety* of
    the characters converted
    to '%xx' formated codes.
    </textarea>
    <br>
    <br>
    <br>
    Output Text<br>
    <textarea name="output" cols="79" rows="8" wrap="VIRTUAL"> </textarea>
    <br>
    &nbsp;
    <input type="button" name="Encode" value="Encode">
    &nbsp;
    <input type="button" name="Decode" value="Decode">
    &nbsp;
    <input name="reset" type="Reset" value="Reset">
    </form>
    </body>
    </html>


  • Grant Wagner

    #2
    Re: Encode And Decode entirety of Text or Html to '%xx' format

    Newbie wrote:
    [color=blue]
    > How would I modify this form
    > to encode *all* the characters
    > in the 'source' textarea to the
    > '%xx' format & place result
    > code into the 'output' textarea?
    > (cross browser compatable)
    >
    > Any help is appreciated.
    >
    > Regards.
    >
    > <html>
    > <head>
    > <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    > <title>Encode And Decode Entire Text or Html to '%xx' format
    > </title>
    > </head>
    > <body>
    > Text and Html "%xx" Converter<br>
    > <form name="form1" method="post" action="">
    > Original Text:<br>
    > <textarea name="source" cols="79" rows="8" wrap="VIRTUAL"> Original Text or Html code
    > to have the *entirety* of
    > the characters converted
    > to '%xx' formated codes.
    > </textarea>
    > <br>
    > <br>
    > <br>
    > Output Text<br>
    > <textarea name="output" cols="79" rows="8" wrap="VIRTUAL"> </textarea>
    > <br>
    > &nbsp;
    > <input type="button" name="Encode" value="Encode">
    > &nbsp;
    > <input type="button" name="Decode" value="Decode">
    > &nbsp;
    > <input name="reset" type="Reset" value="Reset">
    > </form>
    > </body>
    > </html>[/color]

    <script type="text/javascript">
    function encode(f) {
    var ta = f.elements['source'].value;
    var hex;
    var s = [];
    for (var i = 0; i < ta.length; i++) {
    hex = (ta.charCodeAt( i) % 256).toString(1 6);
    s.push((hex.len gth < 2 ? '0' : '') + hex);
    }
    f.elements['output'].value = '%' + (s.join('%')).t oUpperCase();
    }
    function decode(f) {
    var ta = f.elements['source'].value.split(/%/);
    var s = [];
    for (var i = 0; i < ta.length; i++) {
    s.push(String.f romCharCode(par seInt(ta[i], 16)));
    }
    f.elements['output'].value = s.join('');
    }
    </script>

    <input type="button" name="Encode" value="Encode" onclick="encode (this.form);">
    <input type="button" name="Decode" value="Decode" onclick="decode (this.form);">

    I hope I get a good mark from your teacher.

    --
    Grant Wagner <gwagner@agrico reunited.com>
    comp.lang.javas cript FAQ - http://jibbering.com/faq




    Comment

    • Newbie

      #3
      Re: Encode And Decode entirety of Text or Html to '%xx' format

      "Grant Wagner" <gwagner@agrico reunited.com> wrote in message news:410944D1.E 9D4C93A@agricor eunited.com...
      | Newbie wrote:
      |
      | > How would I modify this form
      | > to encode *all* the characters
      | > in the 'source' textarea to the
      | > '%xx' format & place result
      | > code into the 'output' textarea?
      | > (cross browser compatable)
      | >
      | > Any help is appreciated.
      | >
      | > Regards.

      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      <title>Encode And Decode Entire Text or Html to '%xx' format
      </title>
      </head>
      <body>
      <script type="text/javascript">
      <!--
      function encode(f) {
      var ta = f.elements['source'].value;
      var hex;
      var s = [];
      for (var i = 0; i < ta.length; i++) {
      hex = (ta.charCodeAt( i) % 256).toString(1 6);
      s.push((hex.len gth < 2 ? '0' : '') + hex);
      }
      f.elements['output'].value = '%' + (s.join('%')).t oUpperCase();
      }
      function decode(f) {
      var ta = f.elements['source'].value.split(/%/);
      var s = [];
      for (var i = 0; i < ta.length; i++) {
      s.push(String.f romCharCode(par seInt(ta[i], 16)));
      }
      f.elements['output'].value = s.join('');
      }
      //-->
      </script>
      Text and Html "%xx" Converter<br>
      <form name="form1" method="post" action="">
      Original Text:<br>
      <textarea name="source" cols="79" rows="8" wrap="VIRTUAL"> Original Text or Html code
      to have the *entirety* of
      the characters converted
      to '%xx' formated codes.
      </textarea>
      <br>
      <br>
      <br>
      Output Text<br>
      <textarea name="output" cols="79" rows="8" wrap="VIRTUAL"> </textarea>
      <br>
      &nbsp;
      <input type="button" name="Encode" value="Encode" onclick="encode (this.form);">
      &nbsp;
      <input type="button" name="Decode" value="Decode" onclick="decode (this.form);">
      &nbsp;
      <input type="reset" name="Reset" value="Reset">
      </form>
      </body>
      </html>

      | I hope I get a good mark from your teacher.
      |
      | --
      | Grant Wagner <gwagner@agrico reunited.com>
      | comp.lang.javas cript FAQ - http://jibbering.com/faq

      I'm teaching myself & my teacher was really impressed :-)

      Thanks.

      PS:
      Will this work with 3.xx and 4.xx old JS enabled browsers too?


      Comment

      • Grant Wagner

        #4
        Re: Encode And Decode entirety of Text or Html to '%xx' format

        Newbie wrote:
        [color=blue]
        > <script type="text/javascript">
        > <!--[/color]

        <!-- not needed. Omit.
        [color=blue]
        > f.elements['output'].value = s.join('');
        > }
        > //-->[/color]

        //--> not needed. Omit.
        [color=blue]
        > PS:
        > Will this work with 3.xx and 4.xx old JS enabled browsers too?[/color]

        I would have said it'll work in almost any browser capable of JavaScript and forms, but I just discovered
        that charCodeAt() wasn't implemented until JScript 5.5 <url:
        http://msdn.microsoft.com/library/en...nformation.asp />. That has to be a
        typo. I can't believe Microsoft didn't implement the String#charCode At() method until version 5.5. Anyway,
        assuming that MS URL is right, the code I provided will probably only work in Internet Explorer 5.5 and
        higher, although it should work in Netscape versions as low as 3 <url:
        http://devedge.netscape.com/library/...g.html#1196647 />.

        To make it work in versions of JScript older then 5.5, you'll have to add the following to your code:

        if (!String.protot ype.charCodeAt) {
        String.prototyp e.charCodeAt = function (index) {
        var charAt = this.charAt(ind ex);
        var i = 256;
        while (i--) {
        if (charAt == String.fromChar Code(i)) {
        return i;
        }
        }
        return 0;
        }
        }

        Basically you're rolling your own String#charCode At() method that tests the character against each character
        code from 255 to 0. I tested the method above for some simple cases, it seems to work correctly (until you
        throw something like "String.fromCha rCode(1027).cha rCodeAt2(0)" at it). Of course, if the browser is so old
        that you have to use the above method definition, then it's likely it doesn't support unicode either. And
        quite frankly, all the code I gave you assumes character codes in the range 0..255 (0x00..0xFF). Things would
        be a bit more complicated if you allowed (0x0000..0xFFFF ).

        --
        Grant Wagner <gwagner@agrico reunited.com>
        comp.lang.javas cript FAQ - http://jibbering.com/faq

        Comment

        • Newbie

          #5
          Re: Encode And Decode entirety of Text or Html to '%xx' format

          "Grant Wagner" <gwagner@agrico reunited.com> wrote in message news:410A5D2C.9 F8855B5@agricor eunited.com...
          | Newbie wrote:
          |
          | > <script type="text/javascript">
          | > <!--
          |
          | <!-- not needed. Omit.
          |
          | > f.elements['output'].value = s.join('');
          | > }
          | > //-->
          |
          | //--> not needed. Omit.
          |
          | > PS:
          | > Will this work with 3.xx and 4.xx old JS enabled browsers too?
          |
          | I would have said it'll work in almost any browser capable of JavaScript and forms, but I just discovered
          | that charCodeAt() wasn't implemented until JScript 5.5 <url:
          | http://msdn.microsoft.com/library/en...nformation.asp />. That has to be a
          | typo. I can't believe Microsoft didn't implement the String#charCode At() method until version 5.5. Anyway,
          | assuming that MS URL is right, the code I provided will probably only work in Internet Explorer 5.5 and
          | higher, although it should work in Netscape versions as low as 3 <url:
          | http://devedge.netscape.com/library/...g.html#1196647 />.
          |
          | To make it work in versions of JScript older then 5.5, you'll have to add the following to your code:
          |
          | if (!String.protot ype.charCodeAt) {
          | String.prototyp e.charCodeAt = function (index) {
          | var charAt = this.charAt(ind ex);
          | var i = 256;
          | while (i--) {
          | if (charAt == String.fromChar Code(i)) {
          | return i;
          | }
          | }
          | return 0;
          | }
          | }
          |
          | Basically you're rolling your own String#charCode At() method that tests the character against each character
          | code from 255 to 0. I tested the method above for some simple cases, it seems to work correctly (until you
          | throw something like "String.fromCha rCode(1027).cha rCodeAt2(0)" at it). Of course, if the browser is so old
          | that you have to use the above method definition, then it's likely it doesn't support unicode either. And
          | quite frankly, all the code I gave you assumes character codes in the range 0..255 (0x00..0xFF). Things would
          | be a bit more complicated if you allowed (0x0000..0xFFFF ).
          |
          | --
          | Grant Wagner <gwagner@agrico reunited.com>
          | comp.lang.javas cript FAQ - http://jibbering.com/faq

          Thanks again for all of your help.
          It is greatly appreciated that you
          have taken the time & even went
          to the trouble of checking MS &
          Netscape, which I did not expect.

          Kindest Regards.

          PS: My teacher thanks you also :-)


          Comment

          Working...