Thursday, November 1, 2012

JavaScriptStringEncode : Javascript Compitable encoding

Hello,

Recently I was facing very interesting issue. and the solution for the issue was introduced in the new updates in asp.net framework 4.0.

Generally when we try to render HTML content on page clientside/ by javascript, it gives error of Potential dangerous content error.

http://msdn.microsoft.com/en-us/library/ms972967.aspx

There are lot's solutions but have to compromise security of page.
by setting

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs"
    Inherits="_default" ValidateRequest="false"%>


 But now we have perfect solution which is introduced in .net framework 4.0

HttpUtility.JavaScriptStringEncode Method


URL encoding makes sure that all browsers correctly encodes text in URL strings. Few Characters such as a question mark (?), ampersand (&), slash (/), and spaces might be truncated or corrupted by some browsers. Therefore, these characters must be encoded in a elements or in query strings where the strings can be re-sent by a browser in a request string.

refer following  MSDN link
http://msdn.microsoft.com/en-us/library/system.web.httputility.javascriptstringencode%28v=vs.100%29.aspx

http://msdn.microsoft.com/en-us/library/dd991914%28v=vs.100%29.aspx

Hope this helps