Question

Although I know that bgcolor is not the best thing to use to change a web page's color dynamically, the CSS route does not appear to allow numbers like bgcolor does (unless I am mistaken). My code is as follows:

<!DOCTYPE html>
<html>
  <head>
    <title>Title</title>
    <script>
      while(1===1){
        confirm("You will click this";)
        document.write("<h1>HELLO</h1>");
        document.bgcolor = bgcolor;
      } 
    </script>
  </head>
  <body></body>
</html>

I also know that my code is in an infinite loop.

Was it helpful?

Solution

Can you try document.body.style.background instead of document.bgcolor?

Also, I have to ask, why would you need the infinite loop there?

OTHER TIPS

you can look into this for reference

https://developer.mozilla.org/en-US/docs/Web/API/document.bgColor

where it says:

document.bgColor is deprecated in DOM Level 2 HTML. The recommended alternative is use of the CSS style background-color which can be accessed through the DOM with document.body.style.backgroundColor. Another alternative is document.body.bgColor, although this is also deprecated in HTML 4.01 in favor of the CSS alternative.

so if you go to console and write like this, color will be changed

document.body.style.backgroundColor = "darkblue"

Along with what others have said, it appears you have a syntax error on line 7, which may be causing you issues; the semicolon should come after the parenthesis.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top