Solved

JavaScript: using quotation marks inside onload event

Posted on 2011-03-11
3
332 Views
Last Modified: 2012-05-11
My code below works, however I want to place a pair of double quotes around the image in the onload event.


This:
src=http://mirrors.creativecommons.org/blimg/wikimedia-commons-poty-2007.jpg

Should be this:
src="http://mirrors.creativecommons.org/blimg/wikimedia-commons-poty-2007.jpg"

When I add the quotation marks, the image breaks.  I do not want to use single quotes for this-- I want to know how I can escape and use double quotes.

This does NOT work:
src=\"http://mirrors.creativecommons.org/blimg/wikimedia-commons-poty-2007.jpg\"





<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
</head>
<body>

<div id="xyz"></div>

<script type="text/javascript">
/*<![CDATA[*/

document.write('<img src="http://mirrors.creativecommons.org/blimg/wikimedia-commons-poty-2007.jpg" onload="document.getElementById(\'xyz\').innerHTML=\'<img src=http://mirrors.creativecommons.org/blimg/wikimedia-commons-poty-2007.jpg />\'" alt="Load Image" />');

/*]]>*/
</script>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
3 Comments
 
LVL 15

Expert Comment

by:Tomarse111
ID: 35112713
Have you tried using double double quotes as in ""yourimage"". It should escape them?
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 35112768
Trick is to quote it so that it is a runtime expression, and use Chr(34) to insert the double quotes:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
</head>
<body>

<div id="xyz"></div>

<script type="text/javascript">
/*<![CDATA[*/

document.write('<img src="http://mirrors.creativecommons.org/blimg/wikimedia-commons-poty-2007.jpg" onload="document.getElementById(\'xyz\').innerHTML=\'<img src=\'+String.fromCharCode(34)+\'http://mirrors.creativecommons.org/blimg/wikimedia-commons-poty-2007.jpg\'+String.fromCharCode(34)+\' />\'" alt="Load Image" />');

/*]]>*/
</script>

</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 35112799
you may use :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
</head>
<body>

<div id="xyz"></div>

<script type="text/javascript">
/*<![CDATA[*/

document.write("<img src=\"http://mirrors.creativecommons.org/blimg/wikimedia-commons-poty-2007.jpg\" onload=\"document.getElementById('xyz').innerHTML='<img src=http://mirrors.creativecommons.org/blimg/wikimedia-commons-poty-2007.jpg />'\" alt=\"Load Image\" />");

/*]]>*/
</script>

</body>
</html>

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jQuery Scroll To Top 5 40
Add shadow behind div 5 25
message Alert on an empty search 10 23
Help with Javascript to round decimal points to 2 13 18
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question