put statement javascript as nested

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oranoos3000
    New Member
    • Jan 2009
    • 107

    put statement javascript as nested

    hi
    i d like to create a table of safe color for web as online.
    i show a table of safe color on the page and i want to by clicking user on the links
    background-color document is changed but sorry my script doesnt work
    and by running that in Mozila firefox in menu tools->javascirpt consol
    in line that function bg valu send show error
    my script is as follow:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body onLoad="document.bgColor='#ff0000'">
    <script type="text/javascript">
    <!--
    function bg(c)
    {
    document.bgColor="'#"+c+"'";
    }
    var t="";
    ar_color=new Array('00','33','66','99','CC','FF');
    t+='<table>'
    for(i=0;i<6;++i)
    {
    for(j=0;j<6;++j)
    {
    t+='<tr>';
    for(k=0;k<6;++k)
    {
    color=ar_color[i]+ar_color[j]+ar_color[k];
    t+='<td bgColor="#'+color+'"><a href="#" onMouseOver="bg('+color+')" > #'+color+'</a></td>';
    }
    t+='</tr>';
    }
    }
    t+='</table>';
    document.write(t);
    document.close();
    //-->
    </script>
    </body>
    </html>
    thanks for your help
    Last edited by Dormilich; Aug 22 '09, 08:19 AM. Reason: added [code] tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    change line 14 to:

    Code:
    document.bgColor="#"+c;
    and line 27 to:

    Code:
    t+='<td bgColor="#'+color+'"><a href="#" onclick="bg(\''+color+'\')" > #'+color+'</a></td>';
    kind regards

    Comment

    • oranoos3000
      New Member
      • Jan 2009
      • 107

      #3
      hi
      thanks for your answer but why

      Code:
      bg(\''+color+'\')"
      ?
      and why not

      Code:
      onclick="bg('"+color+"')"
      in follow line
      Code:
      t+='<td bgColor="#'+color+'"><a href="#" onclick="bg(\''+color+'\')" > #'+color+'</a></td>';
      Last edited by gits; Sep 1 '09, 08:53 AM. Reason: added code tags

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        when you use it your mentioned way then you might see that the string is terminated too early and throws a JavaScript error, it would look like this:

        Code:
        t+='<td bgColor="#'+color+'"><a href="#" onclick="bg('"+color+"')"
        so after bg(' the string is terminated unless you escape the quote correctly

        kind regards

        Comment

        Working...