Encode HTML Objects

I am parsing some data from feedburner which contains HTML objects. I am trying to encode HTML objects using jQuery as such:

var encodedStr = data['1']['result']['content']; // content with HTML entities $("#content").html(encodedStr).text(); 

but without results.

Here is what he analyzed: http://jsbin.com/ihadam/1/edit

+4
source share
2 answers

Basically you should encode your html objects into html as such:

 var encodedStr = data['1']['result']['content']; var a = $("#content").html(encodedStr).text(); 

Then get the encoded text and apply it as html () as such:

 $("#content").html(a); 

That should work.

Demo: http://jsbin.com/ihadam/9/edit

+2
source

You can keep your jQuery fancy with pure JavaScript features.

Sometimes you just want to encode every character ... This function replaces "everything but nothing" in regxp.

 function encode(e){return e.replace(/[^]/g,function(e){return"&#"+e.charCodeAt(0)+";"})} 

 function encode(w) { return w.replace(/[^]/g, function(w) { return "&#" + w.charCodeAt(0) + ";"; }); } test.value=encode(document.body.innerHTML.trim()); 
 <textarea id=test rows=11 cols=55>www.WHAK.com</textarea> 
0
source

All Articles