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>

javascript escape function

Search for: javascript escape function


* @ - _ + . /

try encodeURI() or encodeURIComponent()


var somestring="lblkjlkj  + alkdjj  @";

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

javascript escape or convert html or xml characters

Search for: javascript escape or convert html or xml characters

discussion on SOF

escape html jquery

Search for: escape html jquery


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.