http://www.kingsley-hughes.com/tech/script/javascript/esc.asp

Some times when server side needs to write out a string variable that initializes a javascript string it is important to know how to escape strings.

The usual html escape and unescape are either too heavy handed and require probably special handling.

A good approach may be just use the back slashes to skip the quote double or single quotes.

Apparently this doesn't work


<form name="mainform">
<input type="text" name="noteNameTextBox" size="57" value="\"hello\"">
</form>

But this does


<form name="mainform">
<input type="text" name="noteNameTextBox" size="57" value="">
</form>

<script>
document.mainform.noteNameTextBox.value="\"hello\"";
</script>

satya - Friday, February 24, 2012 11:52:16 AM

javascript escape function

javascript escape function

Search for: javascript escape function

satya - Friday, February 24, 2012 12:09:57 PM

escape won't replace the following components


* @ - _ + . /

satya - Friday, February 24, 2012 12:11:00 PM

try encodeURI() or encodeURIComponent()

try encodeURI() or encodeURIComponent()

satya - Sat May 05 2012 14:43:16 GMT-0400 (Eastern Daylight Time)

In fact use encodeURIComponent() for encoding argument values


var somestring="lblkjlkj  + alkdjj  @";

var x = "arg1=" + encodeURIComponent(somestring);

satya - 4/21/2014 11:56:30 AM

javascript escape or convert html or xml characters

javascript escape or convert html or xml characters

Search for: javascript escape or convert html or xml characters

satya - 4/21/2014 12:00:08 PM

discussion on SOF

discussion on SOF

satya - 4/21/2014 12:01:17 PM

escape html jquery

escape html jquery

Search for: escape html jquery

satya - 4/21/2014 3:04:22 PM

here is an escape html function


function escapeHTML(text)
    {
       var chr = { '"': '"', '&': '&', '<': '<', '>': '>' };
       function abc(a)
       {
          return chr[a];
       }
       return text.replace(/[\"&<>]/g, abc);
    }

Notice that there is no close quotation marks for the replace function argument of a string.