Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Regular Coder
    Join Date
    Sep 2010
    Far far away
    Thanked 16 Times in 16 Posts

    JavaScript html entities

    Last year my colleague asked me help to implement an insertion of html-entities. This task was interesting but the searching was attractive because there are many copies of the manual parsing of the datum list of entities - phpjs.org :: html_entity_decode. It is bombastic code, in our opinion is unreasonable - we was looking for the compact and easy code using internal features of browsers. We found the solution completely satisfying our requests - it is compact, easy and uses internal parsing algorithms of browsers.

    text in Russian - http://with-love-from-siberia.blogsp...-entities.html
    source code - http://code.google.com/p/jsxt/source.../web/Entity.js

    var Entity = {
        encode: function(value)
            var div = document.createElement('div');
            var text = document.createTextNode(value);
            return div.innerHTML.replace(/"/g, '"').replace(/'/g, ''');
        decode: function(value)
            var textarea = document.createElement('textarea');
            textarea.innerHTML = value;
            return textarea.value;
    var original = '& is <ampersand>';
    var encoded = Entity.encode(original);
    var decoded = Entity.decode(encoded);
    alert([original, encoded, decoded].join('\n'));
    // will output
    // & is <ampersand>
    // &amp; is &lt;ampersand&gt;
    // & is <ampersand>
    Last edited by siberia-man; 01-02-2011 at 07:31 PM.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts