Jump to content

Escaping single and double quotes


lemmin

Recommended Posts

I am trying to escape single and double quotes and I have come across the strangest anomalies. I can get each to escape in separate cases, but never together. Here is what I mean;

 

If the outer-most quotes are single and the inner ones are double, double quotes will escape but not single quotes:

//This works:
onMouseOver='self.status="te\"st";'
//But this doesn't:
onMouseOver='self.status="te\'st";'

 

Similarly, if the outer-most quotes are double and the inner ones are single, single quotes will escape but not double quotes:

//This works:
onMouseOver="self.status='te\'st';"
//But this doesn't:
onMouseOver="self.status='te\"st';"

 

However, if I only use one set of qoutes everything works as expected, but it seems strange to not have the event code in quotes:

//This works:
onMouseOver=self.status="te\"\'st";

 

Is there anyway to get both types of quotes to escape inside tiered quotes? If not, should I be worried about not putting the event code in quotes as the last code example shows?

 

Thanks for any help.

Link to comment
Share on other sites

It makes perfect sens to me. I think you are confusing the parameter value and a string value. You don't escape quortes for the paramater, but you do for the string. Here is one of the examples where you say it doesn't work:

 

Here is the parameter value:

onMouseOver="self.status='te\"st';"

 

Here is the string value:

onMouseOver="self.status='te\"st';"

It is defined with single quotes, so any single quotes in the string must be escaped.

Link to comment
Share on other sites

I understand what the string is. I want to be able to put a quote character in the string, but the method I found to allow me to this disallows me to use a single quote character in the string, likewise, the other way around is a problem, too.

 

In the example you provided, how would I make a double quote show up in the status bar?

 

Thanks for the help.

Link to comment
Share on other sites

Just use the HTML character code(s):

 

<html>
<Head></head>

<body>

<div onMouseOver="self.status='A test with (\') single quote.';">This shows a single quote</div><br>
<div onMouseOver="self.status='A test with (") double quote.';">This shows a double quote</div><br>
<div onMouseOver="self.status='A test with (\') (") both quotes.';">This shows both quote makrs</div><br>

</body>
</html>

Link to comment
Share on other sites

Thanks, that would be a good solution, but I was curious if there was actually a way to make the escaping work as expected. It seems strange to me that it is only possible to escape one or the other. I figured there was a different syntax than the way I was doing it that would allow normal escaping of those characters.

 

Thanks for the help.

Link to comment
Share on other sites

I hear what you're saying. I thought thre was a way to escape the outer quotes as well. But I wasn't able to figure out a solution after several minutes of testing. So, as long as there is an acceptable solution I wouldn't waste too much time trying to figure it out.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.