473,503 Members | 12,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Backslash does NOT escape Apostrophe

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
3 22216
> 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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6582
by: Ralph Freshour | last post by:
I have a PHP web app using MySQL - when I save a .jpg file named test's.jpg I see that the filename on the unix server is: test\'s.jpg - the filename I end up saving in my SQL table is named...
15
7956
by: soup_or_power | last post by:
Hello All: I'm having a whale of a problem with the following code. When the words beginning with sugg contain an escaped single-quote (\' ==> back-slash followed by quote) the script causes an...
4
7458
by: Guadala Harry | last post by:
I need to place the following into a string... How can I properly escape the % " / < and > characters? <table width="100%" border="0" cellspacing="0" cellpadding="4px" class="hfAll"></Table> ...
2
19832
by: Christopher Ireland | last post by:
Hi -- Any ideas on this one please ... The following code works as expected: private void Form1_Load(object sender, System.EventArgs e) { string text = "The quick brown"+ " FOX " +"jumps over...
11
1278
by: Peted | last post by:
I see many c# code samples that relate to strings or using strings in file paths, that have an @ sign. eg string path1 = @"mydir"; string path2 = @"\mydir"; what does the @ do ?
12
4140
by: Johnny BeGood | last post by:
Hi All, When a user enters an Apostrophe into a text area field on a form, i.e. didn't, it mucks with odbc as follows Syntax error (missing operator) in query expression ''didn't', Whats...
3
4900
by: pitchblack408 | last post by:
Hello I am using excel as my database and when I do an insert there is an apostrophe that appears in the cell where a string was inserted. For example '(474)343-3433 It appears that the...
4
2266
by: Razzbar | last post by:
I'm working on a bookmarklet that grabs information from a page and submits it to a server. Yet another social bookmarking application. I'm having trouble with page titles that include an...
2
31175
by: bharath_r | last post by:
Hi, I would like to know how can i escape a .(dot) in a regular expression in javascript. I tried using 'backslash' to escape the 'dot' but its not working. By default a 'dot' takes any...
0
7212
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7296
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5604
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.