By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,030 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,030 IT Pros & Developers. It's quick & easy.

Backslash does NOT escape Apostrophe

P: n/a
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">
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!!!
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> 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">
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>


So, pos contains "DMACC, It's the Smart Thing to Do."

So, "alt='" + pos + "'>" is alt='DMACC, It's the Smart Thing to Do.'> which
has an unquoted single quote in a single quote delimited string.

Generally, I like to use single quote for JavaScript strings and double quote
for HTML features. So, 'alt="' + pos + '">' is alt="DMACC, It's the Smart
Thing to Do.">

I like to use a string.quote() method that automatically escapes things, so
everything always works. So, 'alt=' + pos.quote() + '>' is alt="DMACC, It's
the Smart Thing to Do.">

A quote method can be found here:
http://www.crockford.com/javascript/remedial.html

Jul 20 '05 #2

P: n/a
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.'
Jul 20 '05 #3

P: n/a
"Douglas Crockford" <no****@laserlink.net> wrote in message news:<bj**********@sun-news.laserlink.net>...
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">
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>


So, pos contains "DMACC, It's the Smart Thing to Do."

So, "alt='" + pos + "'>" is alt='DMACC, It's the Smart Thing to Do.'> which
has an unquoted single quote in a single quote delimited string.

Generally, I like to use single quote for JavaScript strings and double quote
for HTML features. So, 'alt="' + pos + '">' is alt="DMACC, It's the Smart
Thing to Do.">

I like to use a string.quote() method that automatically escapes things, so
everything always works. So, 'alt=' + pos.quote() + '>' is alt="DMACC, It's
the Smart Thing to Do.">

A quote method can be found here:
http://www.crockford.com/javascript/remedial.html

Thanks much. Didn't try the quote method but turned my single and
double quotes around and I didn't even use the backslash
and it worked like this:
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 + '">');
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.