You need to escape the string you are writing out into DoEdit to scrub out the double-quote characters. They are causing the onclick HTML attribute to close prematurely.
Using the JavaScript escape character, \, isn't sufficient in the HTML context. You need to replace the double-quote with the proper XML entity representation, ".
You need to escape the string you are writing out into DoEdit to scrub out the double-quote characters. They are causing the onclick HTML attribute to close prematurely.
Using the JavaScript escape character, \, isn't sufficient in the HTML context. You need to replace the double-quote with the proper XML entity representation, ".
" would work in this particular case, as suggested before me, because of the HTML context.
However, if you want your JavaScript code to be independently escaped for any context, you could opt for the native JavaScript encoding:
' becomes \x27
" becomes \x22
So your onclick would become:DoEdit('Preliminary Assessment \x22Mini\x22');
This would work for example also when passing a JavaScript string as a parameter to another JavaScript method (alert() is an easy test method for this).
I am referring you to the duplicate Stack Overflow question, How do I escape a string inside JavaScript code inside an onClick handler?.
I need help with escaping literal quotes
Escaping Single and Double Quotes in a generated String - JavaScript - SitePoint Forums | Web Development & Design Community
Escaping Literal Quotes in Javascript - Stack Overflow
JavaScript - How escape quotes in a var to pass data through Json - Stack Overflow
Videos
many things possible:
- use character code 34 (
String.fromCharCode(34)) and concatanate - double escape
"\\\"" - use single quotes to quote double quotes
'"'
probably more…
var q=String.fromCharCode(34);
document.write(q+"quoted text, using a variable with String.fromCharCode(34)"+q+"<br>");
document.write("\"quoted text, single escaped quotes\"<br>");
document.write('"quoted text, quotes in single quotes"<br>');
document.write("you may use document.write(\"\\\"quoted text, single escaped quotes, displayed after double escaping\\\"\");<br>");
Put it like this:
alert("I am a \"double quoted\" string inside \"double quotes\".");
Use the replace() method:
function esc_quot(text)
{
return text.replace("\"", "\\\"");
}
data: '{ id: "' + esc_quot(news_id) + '", title: "' + esc_quot(news_title) + '", body: "' + esc_quot(news_body) + '" }',
Rather than using one-off code, go with a Javascript JSON encoder (such as provided by MooTools' JSON utility or JSON.js), which will take care of encoding for you. The big browsers (IE8, FF 3.5+, Opera 10.5+, Safari & Chrome) support JSON encoding and decoding natively via a JSON object. A well-written JSON library will rely on native JSON capabilities when present, and provide an implementation when not. The YUI JSON library is one that does this.
data: JSON.stringify({
id: news_id,
title: news_title,
body: news_body
}),