Function to get HTML entities?

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

    Function to get HTML entities?

    Hi,

    Is there a Javascript way of taking a string of text and encoding it
    such that its HTML entities are represented? For example, "<" would
    be represented as "&lt;"?

    Thanks, - Dave
  • Martin Honnen

    #2
    Re: Function to get HTML entities?

    laredotornado wrote:
    Is there a Javascript way of taking a string of text and encoding it
    such that its HTML entities are represented? For example, "<" would
    be represented as "&lt;"?
    Well assuming you have script in the browser and HTML document you can
    use an approach like this:

    function htmlEscape (string) {
    var div = document.create Element('div');
    div.appendChild (document.creat eTextNode(strin g));
    return div.innerHTML;
    }

    // use like this

    htmlEscape('a < b && b c')

    // result: a &lt; b &amp;&amp; b &gt; c


    There are however lots of HTML entities defined in the HTML 4 DTD that
    the above approach does not cover. So it depends on which characters
    exactly you want to replace with entity references.


    --

    Martin Honnen

    Comment

    • laredotornado

      #3
      Re: Function to get HTML entities?

      On Jul 22, 10:42 am, Martin Honnen <mahotr...@yaho o.dewrote:
      laredotornadowr ote:
      Is there a Javascript way of taking a string of text and encoding it
      such that its HTML entities are represented?  For example, "<" would
      be represented as "&lt;"?
      >
      Well assuming you have script in the browser and HTML document you can
      use an approach like this:
      >
      function htmlEscape (string) {
         var div = document.create Element('div');
         div.appendChild (document.creat eTextNode(strin g));
         return div.innerHTML;
      >
      }
      >
      // use like this
      >
      htmlEscape('a < b && b c')
      >
      // result: a &lt; b &amp;&amp; b &gt; c
      >
      There are however lots of HTML entities defined in the HTML 4 DTD that
      the above approach does not cover. So it depends on which characters
      exactly you want to replace with entity references.
      >
      --
      >
              Martin Honnen
             http://JavaScript.FAQTs.com/
      Thanks for this excellent function, Martin. Just so the state of the
      world is the same as when I entered it, how do I remove the div I
      just created from the existing DOM after I get the HTML vals?

      - Dave

      Comment

      • Martin Honnen

        #4
        Re: Function to get HTML entities?

        laredotornado wrote:
        Thanks for this excellent function, Martin. Just so the state of the
        world is the same as when I entered it, how do I remove the div I
        just created from the existing DOM after I get the HTML vals?
        The div element object is created inside the function but never inserted
        anywhere in the document so there is no need to remove it from the document.

        --

        Martin Honnen

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Function to get HTML entities?

          laredotornado wrote:
          Is there a Javascript way of taking a string of text and encoding it
          such that its HTML entities are represented? For example, "<" would
          be represented as "&lt;"?
          The most efficient, reliable and easy to maintain way nowadays is probably

          function htmlEncode(s)
          {
          return s.replace(
          /[<>&]/g,
          function(m) {
          return "&" + m.charCodeAt(0) + ";";
          });
          }


          PointedEars
          --
          var bugRiddenCrashP ronePieceOfJunk = (
          navigator.userA gent.indexOf('M SIE 5') != -1
          && navigator.userA gent.indexOf('M ac') != -1
          ) // Plone, register_functi on.js:16

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: Function to get HTML entities?

            Martin Honnen wrote:
            laredotornado wrote:
            >Is there a Javascript way of taking a string of text and encoding it
            >such that its HTML entities are represented? For example, "<" would
            >be represented as "&lt;"?
            >
            Well assuming you have script in the browser and HTML document you can
            use an approach like this:
            >
            function htmlEscape (string) {
            var div = document.create Element('div');
            div.appendChild (document.creat eTextNode(strin g));
            return div.innerHTML;
            }
            This rather bloated, inefficient, and unreliable approach mixes proprietary
            and standards-compliant features without previous runtime feature test. It
            can be safely recommended against.


            PointedEars
            --
            Anyone who slaps a 'this page is best viewed with Browser X' label on
            a Web page appears to be yearning for the bad old days, before the Web,
            when you had very little chance of reading a document written on another
            computer, another word processor, or another network. -- Tim Berners-Lee

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: Function to get HTML entities?

              Thomas 'PointedEars' Lahn wrote:
              laredotornado wrote:
              >Is there a Javascript way of taking a string of text and encoding it
              >such that its HTML entities are represented? For example, "<" would
              >be represented as "&lt;"?
              >
              The most efficient, reliable and easy to maintain way nowadays is probably
              >
              function htmlEncode(s)
              {
              return s.replace(
              /[<>&]/g,
              function(m) {
              return "&" + m.charCodeAt(0) + ";";
              });
              }
              Supplemental: This does not meet the described outcome exactly, but it works
              anyway (sometimes even better than character entity references) and can
              easily be extended for other characters.


              PointedEars
              --
              realism: HTML 4.01 Strict
              evangelism: XHTML 1.0 Strict
              madness: XHTML 1.1 as application/xhtml+xml
              -- Bjoern Hoehrmann

              Comment

              Working...