Single vs Double Quotes (Very Confusing!!)

Discussion in 'JavaScript' started by hiyatran, Jun 6, 2012.

  1. #1
    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.
    <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>
    Code (markup):


    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.

    <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>
    Code (markup):

     
    hiyatran, Jun 6, 2012 IP
  2. igunz

    igunz Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if your document.writeln() using single quote. just add \ (backslash) infront of the single quote in the content.
    example:

    document.writeln('<TR bgcolor="#FFFFFF" onMouseOver="this.bgColor=\'gold\';" onMouseOut="this.bgColor=\'#FFFFFF\';">');
    PHP:
     
    igunz, Jun 8, 2012 IP
  3. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #3
    There's no difference, as far as how 'dynamic' the page is, between your HTML and your Javascript. You're just using Javascript to write static (IOW, non-dynamic) HTML. A dynamic site gets data from some other source (and the data may change, changing the site from one refresh to the next, making it dynamic). Using Javascript to write a static site is wasting effort.
     
    Rukbat, Jun 16, 2012 IP
  4. Ev0Lv

    Ev0Lv Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    Truly correct. :(
     
    Ev0Lv, Jun 17, 2012 IP