By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

put statement javascript as nested

oranoos3000
100+
P: 107
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:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <title>Untitled Document</title>
  6. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  7. </head>
  8.  
  9. <body onLoad="document.bgColor='#ff0000'">
  10. <script type="text/javascript">
  11. <!--
  12. function bg(c)
  13. {
  14. document.bgColor="'#"+c+"'";
  15. }
  16. var t="";
  17. ar_color=new Array('00','33','66','99','CC','FF');
  18. t+='<table>'
  19. for(i=0;i<6;++i)
  20. {
  21. for(j=0;j<6;++j)
  22. {
  23. t+='<tr>';
  24. for(k=0;k<6;++k)
  25. {
  26. color=ar_color[i]+ar_color[j]+ar_color[k];
  27. t+='<td bgColor="#'+color+'"><a href="#" onMouseOver="bg('+color+')" > #'+color+'</a></td>';
  28. }
  29. t+='</tr>';
  30. }
  31. }
  32. t+='</table>';
  33. document.write(t);
  34. document.close();
  35. //-->
  36. </script>
  37. </body>
  38. </html>
thanks for your help
Aug 22 '09 #1
Share this Question
Share on Google+
3 Replies


gits
Expert Mod 5K+
P: 5,390
change line 14 to:

Expand|Select|Wrap|Line Numbers
  1. document.bgColor="#"+c;
  2.  
and line 27 to:

Expand|Select|Wrap|Line Numbers
  1. t+='<td bgColor="#'+color+'"><a href="#" onclick="bg(\''+color+'\')" > #'+color+'</a></td>';
kind regards
Aug 22 '09 #2

oranoos3000
100+
P: 107
hi
thanks for your answer but why

Expand|Select|Wrap|Line Numbers
  1. bg(\''+color+'\')"
?
and why not

Expand|Select|Wrap|Line Numbers
  1. onclick="bg('"+color+"')"
in follow line
Expand|Select|Wrap|Line Numbers
  1. t+='<td bgColor="#'+color+'"><a href="#" onclick="bg(\''+color+'\')" > #'+color+'</a></td>';
Sep 1 '09 #3

gits
Expert Mod 5K+
P: 5,390
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:

Expand|Select|Wrap|Line Numbers
  1. t+='<td bgColor="#'+color+'"><a href="#" onclick="bg('"+color+"')"
so after bg(' the string is terminated unless you escape the quote correctly

kind regards
Sep 1 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.