Hi all,
I am trying to pass variable to a function with nested double quotes.
How do I escape the double quotes?
…
function change(str1){
document.write(str1);
}
…
<TD onmouseover=“javascript:change(‘My name is \“Tom\”’)” onmouseout=“javascript:change(‘’)”>Some text</td>
…
This is not working. Any ideas?
It’s kind of a strange problem. Triple nesting of quotes isn’t possible, so you would think escaping the double quotes around Tom would work, but it doesn’t. As far as I can tell, the rule is: you can only escape quotes that are the same as the next set of quotes surrounding that part of the string.
So, one solution is to start with single quotes:
onmouseover=‘change(“My name is \“Tom\”.”)’
Or, strangely enough, what also works is using the html symbol for a quote: "
The problem with starting with single quotes is that now I will not be able to use a single quote in the string I am outputting. I have hardcoded this example because I assumed there was a simple solution.
What I am trying to do on my website is: allow a user to enter any values(double and single quotes included) into an html text area - then save that info in a db using php - and finally, pass the user’s text (contained in a php variable) to a javascript function for displaying in another area of the webpage(during a mouseover event).
This is also why using the html symbol for the quote is not really good either. I could write a function in php to change to the html symbols, but I was really looking for another solution.
Sorry to confuse you - that was a typo.
Thanks again for the help.
What I am trying to do on my website is: allow a user to enter any values(double and single quotes included) into an html text area - then save that info in a db using php - and finally, pass the user’s text (contained in a php variable) to a javascript function for displaying in another area of the webpage(during a mouseover event).
Try writing the data into the escape() function, which is like the php urlencode() function:
var text = escape(<?php echo $data; ?>);
Then, use unescape() when you want to display the text: