Solved

JavaScript: using quotation marks inside onload event

Posted on 2011-03-11
3
322 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now