• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

JavaScript: using quotation marks inside onload event

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
hankknight
Asked:
hankknight
1 Solution
 
Tomarse111Commented:
Have you tried using double double quotes as in ""yourimage"". It should escape them?
0
 
sjklein42Commented:
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
 
leakim971PluritechnicianCommented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now