treehouse : what would you like to learn today?
Web Design Web Development iOS Development

htmlEntities for JavaScript

Last updated on:

htmlentities() is a PHP function which converts special characters (like <) into their escaped/encoded values (like &lt;). This allows you to show to display the string without the browser reading it as HTML.

JavaScript doesn't have a native version of it. If you just need the very basics to so that the browser won't interpret as HTML, this should work fine (via James Padolsey and I got a a similar idea from David Walsh).

function htmlEntities(str) {
    return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}

The PHP.js project, which is a project to port over all of PHP's native functions to JavaScript, contains an example as well. I tried it and it works, but I've been warned much of the code from that project is poorly written, so I've kept it simple and used the above.

View Comments

Comments

  1. Krinkle
    Permalink to comment#

    I use the following two functions, in my opinion a very solid, complete and easy method.
    This particulary example uses jQuery but the principe isn’t dependant on jQuery:

    // Encode/decode htmlentities
    	function krEncodeEntities(s){
    		return $j("<div/>").text(s).html();
    	}
    	function krDencodeEntities(s){
    		return $j("<div/>").html(s).text();
    	}
  2. Permalink to comment#

    Thanks for the two functions, that helped me out of a pickle working with a serialized query string.

  3. Mark
    Permalink to comment#

    Thanks for the info!! (^_^)b

  4. buzz
    Permalink to comment#

    the problem with the jQuery approach is if your text contains htmlentities, e.g.

    $(”).text(‘&’).html()

    outputs:

    “&amp;”

    maybe not what you want…..

    • buzz
      Permalink to comment#

      entities filtered in above post. should read:

      $(‘<p>’).text(‘&amp;’).html()

      will output

      &amp;amp;

  5. Vinay
    Permalink to comment#

    Thanks it helped..:)?

  6. john
    Permalink to comment#

    Sure it works when you post, but what about saving

  7. Bret
    Permalink to comment#

    The problem with this approach, as Buzz alluded to, is that all ampersands, even if already properly html-ized, will be matched, resulting in things like htmlentities("&amp;") == "&amp;amp;" and htmlentities(htmlentities("<")) == "&amp;lt;" (not that you’d do the latter, but just to illustrate the point). You want to pad the ampersand regex. Just a space will work for most cases, i.e.:

    s.replace(/& /g, '&amp; ')

    or you can get fancier with something along the lines of:

    s.replace(/&[^(#\d+;|a-z+;)]/g, '&amp;')
  8. Jay
    Permalink to comment#

    How does one do the opposite of this? I want to show the interpreted html entity in js. Example:

    
    $(function(){
        $("#demo").formwizard({ 
            textNext: 'Go to step two »'
            }
    );
    
    

    Would like ‘textNext’ to output as Go to step two »

  9. Pimm Hogeling
    Permalink to comment#

    Please note that this doesn’t do the same as the PHP function. The PHP function uses HTML entities; while this function uses XML ones.

    For example, this function leaves Orléans unaltered, while the PHP function turns that string into Orl&eacute;ans.

  10. Shilpa
    Permalink to comment#

    Helped me a lot… krEncodeEntities, krDencodeEntities solved my problem…

Leave a Comment

Use markdown or basic HTML and be nice.