Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Just wanted to let you know that I registered today, and your site is fantastic. I found solutions to problems that I have been encountering for months!..."

Geography

Where in the world do Tek-Tips members come from?

Single vs Double Quotes (Very Confusing!!)Helpful Member!(2) 

hiyatran (TechnicalUser)
6 Jun 12 21:33
The row in the table would highlight whenever I mouse over it.
This works perfectly in HTML but I would like to convert from HTML code to Javascritp code to make it more dynamic.

CODE --> HTML

<html>
<body>

<TABLE border=1>
<TR bgcolor="#FFFFFF" onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#FFFFFF';">
<TD>Mauritania</td><TD>21N</TD><TD>24N</TD><TD>&nbsp;</TD><TD>&nbsp;</TD>
</TR>
<TR bgcolor="#FFFFFF" onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#FFFFFF';">
<TD>Myanmar</td><TD>&nbsp;</TD><TD>M TBA</TD><TD>M TBA</TD><TD>&nbsp;</TD>
</TR>
<TR bgcolor="#FFFFFF" onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#FFFFFF';">
<TD>Nepal</td><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>M TBA</TD>
</TR>
</TABLE>

</body>
</html>


I tried to convert it to Javascript but it only gives me a blank screen.
This is very frustrating because I don't know where I'm going wrong.
Any comments of suggestions would be greatly appreciated.

CODE -->

<html>
<head>
<script type="text/javascript">

function init(){
document.writeln('<TABLE border=1>');

document.writeln('<TR bgcolor="#FFFFFF" onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#FFFFFF';">');
document.writeln('<TD>Mauritania</td><TD>21N</TD><TD>24N</TD><TD>&nbsp;</TD><TD>&nbsp;</TD> </TR>');

document.writeln('<TR bgcolor="#FFFFFF" onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#FFFFFF';">');
document.writeln('<TD>Myanmar</td><TD>&nbsp;</TD><TD>M TBA</TD><TD>M TBA</TD><TD>&nbsp;</TD></TR>');

document.writeln('<TR bgcolor="#FFFFFF" onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#FFFFFF';">');
document.writeln('<TD>Nepal</td><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>M TBA</TD></TR></TABLE>')';
}
</script>
</head>

<body onload="init()">
</body>
</html>

http://snooples.com
http://hotdeal.outingsforless.com
Helpful Member!  vacunita (Programmer)
6 Jun 12 22:11
To put it simply, the single quotes inside single quotes confuses the JS interpreter.

CODE

document.writeln('<TR bgcolor="#FFFFFF" onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#FFFFFF';">');

For all intents and purposes your string ends when it encounters a second single quote, so the rest will cause errors and halts the Js parsing.

To avoid this you'll need to escape any and all single quotes inside the writerln strings by using slashes before the single quotes.

CODE

document.writeln('<TR bgcolor="#FFFFFF" onMouseOver="this.bgColor=\'gold\';" onMouseOut="this.bgColor=\'#FFFFFF\';">');


As a suggestion, I'd avoid using the writeln method, and instead use either the proper createelement methods, or the innerHTML property of the element you wish to place content into.

CODE

function init(TObj) { TObj.innerHTML += '<TR bgcolor="#FFFFFF" onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#FFFFFF';">'; } ... <body onload="init(this)">


*this is a special keyword.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

mmerlinn (Programmer)
7 Jun 12 4:02
I second Phil on escaping the single quotes.

You should also know that if you had enclosed the whole string in double quotes, then you would have had to escape the double quotes instead.

To always be entirely safe, you could always escape all quotes within the initial and final quotes regardless of which type you use, although I am sure Phil would call that overkill.

mmerlinn

http://mmerlinn.com

Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond

Helpful Member!  feherke (Programmer)
7 Jun 12 5:07
Hi

Just for completeness, there is another alternative : character entities.

CODE --> JavaScript

document.writeln('<TR bgcolor="#FFFFFF" onMouseOver="this.bgColor=&quot;gold&quot;;" onMouseOut="this.bgColor=&quot;#FFFFFF&quot;;">'); // or document.writeln('<TR bgcolor="#FFFFFF" onMouseOver="this.bgColor=&apos;gold&apos;;" onMouseOut="this.bgColor=&apos;#FFFFFF&apos;;">');

Feherke.
http://feherke.github.com/

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close