Wednesday, May 8, 2013

Passing down a string with an apostrophe to javascript function

Its like back to  Javascript 101, but sometimes its forgotton and takes up some time to identify whats wrong with the script. Thats what happened to me. I wasn't sure how would I parse my string. Finally got an answer.

 Edit Content

The escape character takes care of the apostrophe, and the HtmlDecode takes care of any browser based compatibility issues

2 comments:

Anonymous said...

You may more than apostrophes to worry about. strings can contain other characters that can mess with javascript such as line breaks and double quotes. If you are in .Net 4 you should use HttpUtility.JavascriptStringEncode instead of the replace function as it will escape all nasty characters.

Usman Suglatwala said...

Thanks for the Tip! That will be useful!