I have a question about escaping characters in JavaScript where I hope you can help. Say, if I have the following JavaScript code:
document.write("<img src=\"http://www.google.com\" />");
Now in the example above, you can see that I started document.writewith double quotes "and therefore why I need to escape the quotes in < img src="" />to make sure JavaScript still considers it to be a string.
But in the example below, you can see that I used a single quote 'to start the statement document.write. My question is do I still need to avoid double quotes? I know the expression will work without this, but what is the best?
document.write('<img src=\"http://www.google.com\" />');
The reason why I ask, I have a conditional statement that I wrote that works when I call (in accordance with the line above), but it does not seem to work and excludes all possibilities as to what might be causing this. I often see such things every day, so any help would be greatly appreciated. Maybe this might be a stupid question, so apologize in advance ...
source
share