Alternative to Server.HtmlEncode in Javascript

You can encode HTML code on the server side using ASP.net’s  Server.HtmlEncode. But if you want to do it on the clientside is there any way to do that. There is no built in function in Javascript to achieve the same. However using Regular Expression, you can do it easily.

Javascript Code:

function  jsHTMLEndcoder(strHTML){

var rtn=null;

rtn=strHTML.toString().replace(/&/g,“&”);

rtn=rtn.replace(/”/g,“"”)

rtn=rtn.replace(/</g,“&lt;”)

rtn=rtn.replace(/>/g,“&gt;”);

return rtn;
}

Reference:

http://andrewu.co.uk/tools/request/

http://blogs.msdn.com/aoakley/archive/2003/11/12/49645.aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s