need to escape HTML chracters with js

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

    need to escape HTML chracters with js

    I need to escape HTML chracters so <test> --> &lt;test&gt;

    Looks like there is no built-in JS function...anyo ne got one handy ?

    thanks


  • Beauregard T. Shagnasty

    #2
    Re: need to escape HTML chracters with js

    In alt.www.webmaster, Boobie wrote:
    [color=blue]
    > I need to escape HTML chracters so <test> --> &lt;test&gt;
    >
    > Looks like there is no built-in JS function...anyo ne got one handy?[/color]

    I don't do JavaScript, but I do have a question: what happens with
    the 10-15% of your visitors who have JavaScript disabled or
    unavailable in their browsers?

    --
    -bts
    -This space intentionally left blank.

    Comment

    • William Tasso

      #3
      Re: need to escape HTML chracters with js

      Writing in news:alt.www.webmaster,comp.lang.javascript
      From the safety of the Shagnasty Software cafeteria
      Beauregard T. Shagnasty <a.nony.mous@ex ample.invalid> said:
      [color=blue]
      > In alt.www.webmaster, Boobie wrote:
      >[color=green]
      >> I need to escape HTML chracters so <test> --> &lt;test&gt;
      >> Looks like there is no built-in JS function...anyo ne got one handy?[/color]
      >
      > I don't do JavaScript, but I do have a question: what happens with the
      > 10-15% of your visitors who have JavaScript disabled or unavailable in
      > their browsers?[/color]

      hrmm, O/P is not specific - this /could/ be a server-side JS question?

      --
      William Tasso - Do not meddle in the affairs of dragons, for you are
      crunchy and good with ketchup.

      g-groups: http://tinyurl.com/e38b2 or http://tinyurl.com/cvsla ?

      Comment

      • Beauregard T. Shagnasty

        #4
        Re: need to escape HTML chracters with js

        In alt.www.webmaster, William Tasso wrote:[color=blue]
        > Writing in news:alt.www.webmaster,comp.lang.javascript From the
        > safety of the Shagnasty Software cafeteria Beauregard T. Shagnasty
        > <a.nony.mous@ex ample.invalid> said:
        >[color=green]
        >> In alt.www.webmaster, Boobie wrote:
        >>[color=darkred]
        >>> I need to escape HTML chracters so <test> --> &lt;test&gt;
        >>> Looks like there is no built-in JS function...anyo ne got one
        >>> handy?[/color]
        >>
        >> I don't do JavaScript, but I do have a question: what happens
        >> with the 10-15% of your visitors who have JavaScript disabled or
        >> unavailable in their browsers?[/color]
        >
        > hrmm, O/P is not specific - this /could/ be a server-side JS
        > question?[/color]

        You have a point, Mr. Tasso. At this late hour, I can't think of a
        reason to do it client-side. (Speaking of late hours, did you just
        crawl out of bed? <g>)

        G'nite, now...

        --
        -bts
        -This space intentionally left blank.

        Comment

        • Jan-Christoph Ihrens

          #5
          Re: need to escape HTML chracters with js

          "Boobie" schrieb:
          [color=blue]
          > I need to escape HTML chracters so <test> --> &lt;test&gt;
          >
          > Looks like there is no built-in JS function...anyo ne got one handy ?[/color]

          What about replace()? ;-)

          But as the others said, you should consider doing this on the server
          side instead of the client side.

          Greetings,
          Jan

          Comment

          • Dr Clue

            #6
            Re: need to escape HTML chracters with js

            Boobie wrote:[color=blue]
            > I need to escape HTML chracters so <test> --> &lt;test&gt;
            >
            > Looks like there is no built-in JS function...anyo ne got one handy ?[/color]


            escapedHTML=szH TML.split("&"). join("&amp;").s plit("<").join( "&lt;").split(" >").join("&gt;" )


            --
            --.
            --=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML, CSS,Javascript
            --=<> http://resume.drclue.net <>=-- AJAX, SOAP, XML, HTTP
            --=<> http://www.drclue.net <>=-- SERVLETS,TCP/IP, SQL
            --.

            Comment

            • Norman L. DeForest

              #7
              Re: need to escape HTML chracters with js


              On Sun, 18 Sep 2005, Boobie wrote:
              [color=blue]
              > I need to escape HTML chracters so <test> --> &lt;test&gt;
              >
              > Looks like there is no built-in JS function...anyo ne got one handy ?
              >
              > thanks[/color]

              Completely untested and could possible reflect my complete
              misunderstandin g of JavaScript:

              function myescape (foo) {
              myfoo = '';
              for (i=0;i<length(f oo);i++)
              { myfoochar = foo.charAt(i);
              if (myfoochar == '<') myfoochar = '&lt;';
              if (myfoochar == '>') myfoochar = '&gt;';
              if (myfoochar == '&') myfoochar = '&amp;';
              // add any other characters you want to handle here
              myfoo += myfoochar;
              }
              return myfoo;
              }

              ....
              ....
              oldbar = '<test>';
              newbar = myescape(oldbar );
              // newbar should be '&lt;test&gt; '
              ....
              ....

              --
              ``Why don't you find a more appropiate newsgroup to post this tripe into?
              This is a meeting place for a totally differnt kind of "vision impairment".
              Catch my drift?'' -- "jim" in alt.disability. blind.social regarding an
              off-topic religious/political post, March 28, 2005

              Comment

              • SpaceGirl

                #8
                Re: need to escape HTML chracters with js


                Boobie wrote:[color=blue]
                > I need to escape HTML chracters so <test> --> &lt;test&gt;
                >
                > Looks like there is no built-in JS function...anyo ne got one handy ?
                >
                > thanks[/color]

                You could fake it using the encodeURI function in JS;

                See -



                It's really for encoding query strings, but I guess it could also be
                used for encoding pages as it converts all non-alpha characters.

                Comment

                • Norman L. DeForest

                  #9
                  Re: need to escape HTML chracters with js


                  On 19 Sep 2005, SpaceGirl wrote:
                  [color=blue]
                  > Boobie wrote:[color=green]
                  > > I need to escape HTML chracters so <test> --> &lt;test&gt;
                  > >
                  > > Looks like there is no built-in JS function...anyo ne got one handy ?
                  > >
                  > > thanks[/color]
                  >
                  > You could fake it using the encodeURI function in JS;
                  >
                  > See -
                  >
                  > http://webcoder.info/reference/URIEsc.html
                  >
                  > It's really for encoding query strings, but I guess it could also be
                  > used for encoding pages as it converts all non-alpha characters.[/color]

                  Unless I am totally misunderstandin g the documentation for the encodeURI
                  function, it won't encode '<' and '>' as '&lt;' and '&gt;' but as '%3C'
                  and '%3E'.

                  --
                  ``Why don't you find a more appropiate newsgroup to post this tripe into?
                  This is a meeting place for a totally differnt kind of "vision impairment".
                  Catch my drift?'' -- "jim" in alt.disability. blind.social regarding an
                  off-topic religious/political post, March 28, 2005

                  Comment

                  • Baconbutty

                    #10
                    Re: need to escape HTML chracters with js

                    Whether you use

                    s.replace(/&/g,"&amp;").repl ace(/</g,"&lt;").repla ce(/>/g,"&gt;");

                    or Array.split (Dr Clue)

                    note that you must remember to do the "&" first, otherwise you will
                    double escape the &lt; and &gt; references.

                    Comment

                    • GreyWyvern

                      #11
                      Re: need to escape HTML chracters with js

                      And lo, Boobie didst speak in alt.www.webmaster,comp.lang.javascript:
                      [color=blue]
                      > I need to escape HTML chracters so <test> --> &lt;test&gt;
                      >
                      > Looks like there is no built-in JS function...anyo ne got one handy ?[/color]

                      String.replace(/</, "&lt;").replace (/>/, "&gt;");

                      Grey

                      --
                      The technical axiom that nothing is impossible sinisterly implies the
                      pitfall corollary that nothing is ridiculous.
                      - http://www.greywyvern.com/orca#sear - Orca Search - PHP/MySQL site
                      search engine

                      Comment

                      • Peroli

                        #12
                        Re: need to escape HTML chracters with js

                        hi Boobie,

                        Boobie wrote:[color=blue]
                        > I need to escape HTML chracters so <test> --> &lt;test&gt;
                        >
                        > Looks like there is no built-in JS function...anyo ne got one handy ?
                        >
                        > thanks[/color]

                        This is taken from "Prototype. js". The best i have ever seen.

                        String.prototyp e.extend({
                        stripTags: function() {
                        return this.replace(/<\/?[^>]+>/gi, '');
                        },

                        escapeHTML: function() {
                        var div = document.create Element('div');
                        var text = document.create TextNode(this);
                        div.appendChild (text);
                        return div.innerHTML;
                        },

                        unescapeHTML: function() {
                        var div = document.create Element('div');
                        div.innerHTML = this.stripTags( );
                        return div.childNodes[0].nodeValue;
                        }
                        });

                        -- Peroli Sivaprakasam

                        Comment

                        • Dr John Stockton

                          #13
                          Re: need to escape HTML chracters with js

                          JRS: In article <DUqXe.81416$EX .22865@twister. nyroc.rr.com>, dated Mon,
                          19 Sep 2005 04:12:19, seen in news:comp.lang. javascript, Beauregard T.
                          Shagnasty <a.nony.mous@ex ample.invalid> posted :
                          [color=blue][color=green]
                          >> hrmm, O/P is not specific - this /could/ be a server-side JS
                          >> question?[/color]
                          >
                          >You have a point, Mr. Tasso. At this late hour, I can't think of a
                          >reason to do it client-side.[/color]

                          There may be no server-side processing. Most of my javascript pages
                          indirectly use replacement of & < > yet none of them use a server
                          except for initial delivery :

                          function SafeHTML(S) {
                          return S.replace(/&/g, "&amp;").
                          replace(/</g, "&lt;").replace (/>/g, "&gt;") }

                          --
                          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                          <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                          Comment

                          • Dr John Stockton

                            #14
                            Re: need to escape HTML chracters with js

                            JRS: In article <V%qXe.150$zQ3. 108@newsread1.n ews.pas.earthli nk.net>,
                            dated Mon, 19 Sep 2005 04:20:05, seen in news:comp.lang. javascript, Dr Clue
                            <ianstormsfw@mi ndspring.com> posted :
                            [color=blue]
                            > ...
                            > .split("&").joi n("&amp;").spli t("<").join("&l t;").split(">") .join("&gt;")
                            >[/color]

                            Have you compared, in various browsers, the speed of that on comparison
                            with the perhaps more obvious RegExp method? I find it to be annoyingly
                            quicker.

                            --
                            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                            <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                            Comment

                            • Jerry Stuckle

                              #15
                              Re: need to escape HTML chracters with js

                              Dr John Stockton wrote:[color=blue]
                              > JRS: In article <DUqXe.81416$EX .22865@twister. nyroc.rr.com>, dated Mon,
                              > 19 Sep 2005 04:12:19, seen in news:comp.lang. javascript, Beauregard T.
                              > Shagnasty <a.nony.mous@ex ample.invalid> posted :
                              >
                              >[color=green][color=darkred]
                              >>>hrmm, O/P is not specific - this /could/ be a server-side JS
                              >>>question?[/color]
                              >>
                              >>You have a point, Mr. Tasso. At this late hour, I can't think of a
                              >>reason to do it client-side.[/color]
                              >
                              >
                              > There may be no server-side processing. Most of my javascript pages
                              > indirectly use replacement of & < > yet none of them use a server
                              > except for initial delivery :
                              >
                              > function SafeHTML(S) {
                              > return S.replace(/&/g, "&amp;").
                              > replace(/</g, "&lt;").replace (/>/g, "&gt;") }
                              >[/color]

                              Exactly. Initial delivery should replace them before they go to the
                              client. Your way will fail when the user has javascript turned off.

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

                              Comment

                              Working...