Backslash does NOT escape Apostrophe

T

Terry Asher

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!!!
 
D

Douglas Crockford

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
 
L

Lasse Reichstein Nielsen

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

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
 
T

Terry Asher

Douglas Crockford said:
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 + '">');
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top