UGH! Using escape characters don't work...anyone know why?

K

KathyB

Hi, not sure this is the right group, but hoping someone may have
experienced this.

I'm passing html text as a parameter to a javascript. When it has an
apostrophe in it, of course it does parse correctly. BTW, using IE6
and msxml3/4.

Even if I manually escape the ' with \' or '' the browser just sees it
as \' or '' and not as an escaped apostrophe.

This is driving me nuts! Everyone just tells me to escape the
character.

Any clues most welcome!

Thanks, Kathy
 
L

Lee

KathyB said:
Hi, not sure this is the right group, but hoping someone may have
experienced this.

I'm passing html text as a parameter to a javascript. When it has an
apostrophe in it, of course it does parse correctly. BTW, using IE6
and msxml3/4.

Even if I manually escape the ' with \' or '' the browser just sees it
as \' or '' and not as an escaped apostrophe.

This is driving me nuts! Everyone just tells me to escape the
character.

Any clues most welcome!

You need to provide more detail. A link to a page or a small sample
of code that shows the problem would help.
Did you mean to say "of course it does NOT parse correctly" ?

How are you passing the HTML text?

Escape characters have no meaning in HTML.
Escape characters only have meaning in literal text.
They are ignored in text that is already in the value of a variable.
For example, the following code will produce a page reading:

Kathy\'s problem
Kathy\'s problem
Kathy's problem

<html>
<body>
<div id="alpha">Kathy\'s problem</div>
<script type="text/javascript">
document.write(document.getElementById("alpha").innerHTML);
document.write("<br>Kathy\'s problem");
</script>
</body>
</html>


<html>
<body>
<div id="alpha">Kathy\'s problem</div>
<script type="text/javascript">
document.write(document.getElementById("alpha").innerHTML);
document.write("<br>Kathy\'s problem");
</script>
</body>
</html>

Kathy\'s problem
Kathy\'s problem
Kathy's problem
 
J

Jim Ley

I'm passing html text as a parameter to a javascript. When it has an
apostrophe in it, of course it does parse correctly.

If it parses correctly, what's your problem...

You might try actually illustrating your problem with code, or a link,
you're not even telling us how you're "passing html text as a
parameter to a javascript"

Jim.
 
K

Kathy Burke

Yes, I DID mean that it does NOT parse correctly. Here is an example of
my script and parameter passed.

<script language="Javascript">
function Anomaly(textIn)
{
newWindow = window.open(('Anomaly.aspx?desc=' +textIn), 'Anomaly',
'width=650,height=700');
}
</script>

<input type="button" value="Anomaly"
onclick="Javascript:Anomaly('Kathy's apostrophe test')">

QUESTION: What does the parameter have to be to not throw an exception
within the script? I've tried \' but it just results in \' in the html
output not just the html source.

I hope this clarifies my question. Thanks for responding!

Kathy
 
L

Lasse Reichstein Nielsen

Kathy Burke said:
Yes, I DID mean that it does NOT parse correctly. Here is an example of
my script and parameter passed.
<input type="button" value="Anomaly"
onclick="Javascript:Anomaly('Kathy's apostrophe test')">
^
The problem is here: ^
That single-quote/apostrophe must be escaped.
QUESTION: What does the parameter have to be to not throw an exception
within the script? I've tried \' but it just results in \' in the html
output not just the html source.

The onclick event should be:
onclick="Anomaly('Kathy\'s apostrophe test')">

Drop the "Javascript:". It doesn't belong there, and it makes no
difference.


Other comments:
<script language="Javascript">

This should be
newWindow = window.open(('Anomaly.aspx?desc=' +textIn), 'Anomaly',

Not all characters are allowed in URL's, so I would suggest changing
"textIn" to "escape(textIn)". That will write characters that are
not allowed in URL's as, e.g., %20 (a space).

/L
 
A

Allan W

(e-mail address removed) (KathyB) wrote

I think what you're asking is how to nest quotes. For instance, if you
want to use document.write() to write out a statement that has a quote
in it.

If so, the answer is that most places that need quotes, can use either
single quotes or double quotes.

document.write("Kathy's page"); // Quoted string contains
apostrophe
shows up as
Kathy's page

document.write('Is "Kathy" home?"); // Quoted string contains
quote
shows up as
Is "Kathy" home?

If you find that you need to use both, you can use the + sign to
concatenate.

document.write('Is this "Kathy' + "'s Page" +'"?');
shows up as
Is this "Kathy's Page"?

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top