javascript - bgcolor vs background-color vs backgroundColor -


i'm trying make "strobe light" heck of in javascript , found code me on internet...but used bgcolor , felt should proper code works if leave bgcolor...so know mean here's original:

<html><head> <title>strobe</title>  <script>  function togglebgcolor() {   document.bgcolor = document.bgcolor == '#ffffff' ? '#000000' : '#ffffff';    settimeout('togglebgcolor()', 70); //in milliseconds } </script> </head>  <body onload='togglebgcolor();'> </body></html> 



and here's changes:


<html><head> <title>strobe</title>  <script>  function togglebgcolor() {   document.body.style.background-color = document.body.style.background-color == '#ffffff' ? '#000000' : '#ffffff';    settimeout('togglebgcolor()', 70); //in milliseconds } </script> </head>  <body onload='togglebgcolor();'> </body></html> 



i've tried changing document.body.style.background-color document.body.style.background , document.body.style.backgroundcolor ...none of them work...what doing wrong?

document.body.style.background-color invalid identifier (well, technically it's valid identifier [document.body.style.background], followed operator [-], followed valid identifier [color], know mean). use document.body.style.backgroundcolor instead. does work, provided other things correct. live example

you've said you've tried that. suspect problem code failing elsewhere. instance, you're comparing '#ffffff':

document.body.style.backgroundcolor = document.body.style.backgroundcolor == '#ffffff' ? '#000000' : '#ffffff'; //                                                                        ^^^^^^^^^^^^ 

the browser may not (probably won't) report in same format use assign color. value come "white" in browsers, , rgb(255, 255, 255) in others, etc. comparison fail, when background color is white. you'd have handle complexity, parsing rgb , doing lookups on color names, etc. —or maintain flag in example above.


off-topic: avoid passing strings settimeout; instead, use function references directly. in case:

settimeout(togglebgcolor, 70); //in milliseconds 

note no quotes, , no () (because call function; want pass reference in, not return value).

if passing arguments (you're not there, completeness), can use function that:

settimeout(function() {     dosomething("foo", "bar"); }, 70); 

Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -