473,320 Members | 1,879 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,320 software developers and data experts.

Escaping quotes withing quotes

I have a problem of escaping quotes in javascript.

Ex:

onclick='alert( "Mister O'Hara" )'
onclick='alert( "Mister O\'Hara" )'

both gives me an error. How would I escape this?

Jul 23 '05 #1
7 21588


du*****@gmail.com wrote:
I have a problem of escaping quotes in javascript.

Ex:

onclick='alert( "Mister O'Hara" )'


onclick="alert('Mister O\'Hara')"
is one way, or use a HTML escape e.g. character reference
onclick='alert("Mister O'Hara")'

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
du*****@gmail.com writes:
I have a problem of escaping quotes in javascript.
Nope :)
onclick='alert( "Mister O'Hara" )'
onclick='alert( "Mister O\'Hara" )'

both gives me an error. How would I escape this?


Your problem is that the outer (single-)quotes are not Javascript
quotes, but HTML quotes. It is the HTML parser that barfs over your
code, not Javascript, so you would need an HTML escape, not the
Javascript escape.

The HTML "escape" of a single quote is the entity ',
so
onclick='alert("Mister O'Hara");'

Alternatively, you could use double quotes in HTML and single in
Javascript, and then use a Javascript escape:
onclick="alert('Mister O\'Hara');"

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #3


Lasse Reichstein Nielsen wrote:

Your problem is that the outer (single-)quotes are not Javascript
quotes, but HTML quotes. It is the HTML parser that barfs over your
code, not Javascript, so you would need an HTML escape, not the
Javascript escape.

The HTML "escape" of a single quote is the entity &apos;,
so
onclick='alert("Mister O&apos;Hara");'


HTML 4.01 does not define an enity by the name apos, the above does not
validate therefore.
Nowadays most modern browsers support apos nevertheless but for instance
Netscape 4 does not. Thus a numeric character reference ' is safer.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #4
Thanks for clarifying that. Using onclick="alert('Mister O\'Hara');"
would solve the problem if the user use ' (single quote), but they can
type in " (double quote). In that case, we have the same problem.

Jul 23 '05 #5
Lasse Reichstein Nielsen wrote:
du*****@gmail.com writes:
I have a problem of escaping quotes in javascript.


Nope :)
onclick='alert( "Mister O'Hara" )'
onclick='alert( "Mister O\'Hara" )'

both gives me an error. How would I escape this?


[...] It is the HTML parser that barfs over your code, not
Javascript, [...]


That is not entirely true. The markup -- dare I say "tag soup" --
parser does what it can to correct this invalid markup which
results in

onclick='alert( "Mister O'
onclick='alert( "Mister O\'

But then the script engine is passed this code when the event fires,
and so:

| Error: Unterminated string literal
|
| alert( "Mister O
| ------------------^
| alert( "Mister O\'
| --------------------^
PointedEars
--
When the power of love overcomes the love
of power, the world will know peace.
-- Jimi Hendrix
Jul 23 '05 #6
> Thanks for clarifying that. Using onclick="alert('Mister O\'Hara');"
would solve the problem if the user use ' (single quote), but they can
type in " (double quote). In that case, we have the same problem.


How is the user inputting "Mister O'Hara"? Maybe you can assign this to a
JavaScript variable. I don't think there is any problem using both single
and double quotes inside the variable. Example (in the body code)-- I
tested this in IE 6.0:
<script type="text/javascript">
var myVar = 'Mister O\'Hara says, "Hi."';
</script>
<p onclick='alert(myVar)'>Click here.</p>
...Jim in Dallas...
Jul 23 '05 #7
du*****@gmail.com wrote:
Thanks for clarifying that. Using onclick="alert('Mister O\'Hara');"
would solve the problem if the user use ' (single quote), but they can
type in " (double quote). In that case, we have the same problem.


If the string data is provided by user input, I doubt you
have to care about that unless you misuse the eval() method.
PointedEars
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: sankofa | last post by:
hi, i can't seem to be able to escape my single quote properly... is it even possible in javascript? this is a portion of my code.. var DLEWIS="Pastor Lewis"; .... Sermon is a yser-defined...
4
by: Stefan Richter | last post by:
How do I encode double quotes and quotes and in a string in VB.NET? It also has to be save for MS SQL Server... Stefan
16
by: Charles Law | last post by:
I have a string similar to the following: " MyString 40 "Hello world" all " It contains white space that may be spaces or tabs, or a combination, and I want to produce an array...
15
by: bill | last post by:
I am trying to write clean code but keep having trouble deciding when to quote an array index and when not to. sometimes when I quote an array index inside of double quotes I get an error about...
12
by: techbuddha | last post by:
Hi I am new to VB. I have an access database that has data that uses the single quote like in (don't) or (g'night). I have create a procedure that will do a search and replace, Which I thought...
2
by: cesco | last post by:
Hi, I have a string which is constructed like this: string = "Butler's 15\" TV" s = """my string goes here "%s" """ % string the single string, unfortunately, gets escaped which is...
2
by: Denise | last post by:
Front end is Access 2002, back end is linked Oracle tables. My users need to describe things in feet and inches and want to use the standard ' and " abbrevations. On a testing form I go to a...
1
by: Yearwood | last post by:
Hi, I'm basically trying to import a CSV into an ACCESS database. Sample date is shown below: "",10173,"Development Manager - Social Economy Sector","Trust Bank",10153,,"Lolalll Pudd","Meet the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.