How can I chain some text contains characters need to escape in HTML?

2 views
Skip to first unread message

Alan She

unread,
Nov 30, 2008, 2:01:35 AM11/30/08
to chainjs
I have a problem when using chain.js in my project.

I load some text by ajax request from the server side, which contains
some character like '<', '>'. Obviously it need to escape according to
HTML rules before put into the HTML.

For example, I have this string from server side
'normal <font color="red">NO RED</font> text'

If I just chain it to the html without any special handling, the
result I saw in browser is 'normal NO RED text', while 'NO RED'
colored in red.
However, if I do an escape html handling in chain, the result I saw in
browser is
'normal &lt;font color=&quot;red&quot;&gt;NO RED&lt;/font&gt; text'.

The way I handle html escape is,

$('#something').items(listOfItems).chain({
'.name' : {
content: function(data) {
return escapeHtml(data);
}
}
});

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

What am I suppose to do to let me see 'normal <font color="red">NO
RED</font> text' in browser?

Thanks for your help!

Rizqi Ahmad

unread,
Nov 30, 2008, 3:38:05 AM11/30/08
to chainjs
Instead of using:

content: function(){}

use

text: ...
Reply all
Reply to author
Forward
0 new messages