Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-02-2011, 06:25 PM   PM User | #1
siberia-man
Regular Coder

 
Join Date: Sep 2010
Location: Far far away
Posts: 122
Thanks: 0
Thanked 16 Times in 16 Posts
siberia-man is on a distinguished road
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

Code:
var Entity = {
    encode: function(value)
    {
        var div = document.createElement('div');
        var text = document.createTextNode(value);
        div.appendChild(text);
        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 06:31 PM..
siberia-man is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:55 AM.


Advertisement
Log in to turn off these ads.