473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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 22207
> 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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.