tl*****@dmacc.edu (Terry Asher) writes:
The following script does NOT escape the Apostrophe.
Meaning when you mouseover the image the Alt tag
says this: DMACC, It and then it stops.
<SCRIPT Language="JavaScript">
It's
<script type="text/javascript">
according to the HTML 4 specification.
var pos = "DMACC, It\'s the Smart Thing to Do.";
document.write("<img name=img5 id=img5 src='/homepage/dmaccstudent" +
Math.floor(Math.random() *20) +
".jpg' WIDTH=145 HEIGHT=230 border=0 ALT='"+pos+"'>");
</SCRIPT>
Please Help! I've been struggling with
this stupid apostrophe for far too long!!!
It is a matter of levels. You have two levels of Javascript here.
The first is the code above. The second is the string you document.write.
You want to escape the quote in the second level code.
First
var pos = "DMACC, It\'s the Smart Thing to Do.";
In this string, the "\" doesn't matter. It merely quotes the ', but
since we are inside double-quotes, that is not necessary. The content
of the resulting string is just
DMACC, It's the Smart Thing to Do.
Now, you combine this string with other strings and write it to a
document. What is written is (with the random giving, e.g., 17):
<img name=img5 id=img5 src='/homepage/dmaccstudent17.jpg'
WIDTH=145 HEIGHT=230 border=0 ALT='DMACC, It's the Smart Thing to Do.'>");
(I broke the line to make room). And the quotes aren't matched.
What you need is for the document.write to write a backslash before
the quote. To do that, you must include a backslash in the string value.
That means that the string literal must include an escaped backslash:
var pos = "DMACC, It\\'s the Smart Thing to Do.";
Good luck
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'