Connecting Tech Pros Worldwide Forums | Help | Site Map

Escaping quotes withing quotes

duwayne@gmail.com
Guest
 
Posts: n/a
#1: Jul 23 '05
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?


Martin Honnen
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Escaping quotes withing quotes




duwayne@gmail.com wrote:
[color=blue]
> I have a problem of escaping quotes in javascript.
>
> Ex:
>
> onclick='alert( "Mister O'Hara" )'[/color]

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/
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Escaping quotes withing quotes


duwayne@gmail.com writes:
[color=blue]
> I have a problem of escaping quotes in javascript.[/color]

Nope :)
[color=blue]
> onclick='alert( "Mister O'Hara" )'
> onclick='alert( "Mister O\'Hara" )'
>
> both gives me an error. How would I escape this?[/color]

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 - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Martin Honnen
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Escaping quotes withing quotes




Lasse Reichstein Nielsen wrote:

[color=blue]
> 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");'[/color]

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/
duwayne@gmail.com
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Escaping quotes withing quotes


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.

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Escaping quotes withing quotes


Lasse Reichstein Nielsen wrote:
[color=blue]
> duwayne@gmail.com writes:[color=green]
>> I have a problem of escaping quotes in javascript.[/color]
>
> Nope :)
>[color=green]
>> onclick='alert( "Mister O'Hara" )'
>> onclick='alert( "Mister O\'Hara" )'
>>
>> both gives me an error. How would I escape this?[/color]
>
> [...] It is the HTML parser that barfs over your code, not
> Javascript, [...][/color]

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
Jimnbigd
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Escaping quotes withing quotes


> Thanks for clarifying that. Using onclick="alert('Mister O\'Hara');"[color=blue]
> 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.[/color]

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...


Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Escaping quotes withing quotes


duwayne@gmail.com wrote:
[color=blue]
> 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.[/color]

If the string data is provided by user input, I doubt you
have to care about that unless you misuse the eval() method.


PointedEars
Closed Thread