...

View Full Version : ’ works, ' doesn't



shlagish
12-19-2004, 08:43 PM
Hello!

I have a page where I use javascript to make some info appear in a box when I hover a link. This is a simplified version of my code:


<html>
<head>
<title>Apostrophe bug</title>
<script type="text/javascript">
<!--

function desc(newText){

document.getElementById('jsLinkDesc').innerHTML=newText;
}

-->
</script>
</head>
<body>

<ul>
<li><a href="#" onmouseover="desc('Description of Link')" onmouseout="desc('Link Description box')">Link</a></li>
<li><a href="#" onmouseover="desc('Description of Link2')" onmouseout="desc('Link Description box')">Link2</a></li>
<li><a href="#" onmouseover="desc('This won't work because of the apostrophe')" onmouseout="desc('Link Description box')">Faulty link</a></li>
</ul>
<div id="jsLinkDesc">Link Description Box</div>
</body>
</html>

As you can see, the "Faulty link"'s onmouseover doesn't work because there is an apostrophe. So I tried changing the apostrophe with &#39;


<html>
<head>
<title>Apostrophe bug</title>
<script type="text/javascript">
<!--

function desc(newText){

document.getElementById('jsLinkDesc').innerHTML=newText;
}

-->
</script>
</head>
<body>

<ul>
<li><a href="#" onmouseover="desc('Description of Link')" onmouseout="desc('Link Description box')">Link</a></li>
<li><a href="#" onmouseover="desc('Description of Link2')" onmouseout="desc('Link Description box')">Link2</a></li>
<li><a href="#" onmouseover="desc('This won&#39;t work because of the apostrophe')" onmouseout="desc('Link Description box')">Faulty link</a></li>
</ul>
<div id="jsLinkDesc">Link Description Box</div>
</body>
</html>

It still doesn't work!!
I then tried with ’


<html>
<head>
<title>Apostrophe bug</title>
<script type="text/javascript">
<!--

function desc(newText){

document.getElementById('jsLinkDesc').innerHTML=newText;
}

-->
</script>
</head>
<body>

<ul>
<li><a href="#" onmouseover="desc('Description of Link')" onmouseout="desc('Link Description box')">Link</a></li>
<li><a href="#" onmouseover="desc('Description of Link2')" onmouseout="desc('Link Description box')">Link2</a></li>
<li><a href="#" onmouseover="desc('This won’t work because of the apostrophe')" onmouseout="desc('Link Description box')">Faulty link</a></li>
</ul>
<div id="jsLinkDesc">Link Description Box</div>
</body>
</html>

THIS works!!!

What's up with all this?
Thanks for any help :)


While I'm posting up my script, I might as well ask a few more questions:

In my script, I use innerHTML. How could I do the same thing using DOM?
My script makes really ugly HTML, how could I make it cleaner? Idealy having something like: <a href="#">Link</a>. I could apply the onmouseover & onmouseout with an external .js file, but the content of each desc('content') is different. Any idea? Also, in the non-simplified version, I also have onfocus and onblur...

Thank you :)

COBOLdinosaur
12-19-2004, 08:58 PM
"desc('This won\'t work because of the apostrophe')"

shlagish
12-20-2004, 02:01 AM
MY HERO!

Thank you, hadn't thought of that.

What about the other questions?

ArcticFox
12-20-2004, 08:32 PM
It’s a different type of quote.

“Hello!” - word/publisher
"Hello!" - computer/internet
It’s different. - word/publisher
It's different. - computer/internet

You can use “word” quotes in JS because the JS only processes its code with "computer" quotes.

shlagish
12-21-2004, 01:49 AM
makes a whole lot of sence, but &#39; doesn't have any computer quote in it...

liorean
12-21-2004, 02:17 AM
It's a question about parsing levels, really. If the error comes from the JavaScript parsing, then escaping it in the HTML doesn't help. The HTML parser will already have replaced it with the corresponding value when it is sent to the JavaScript parser.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum