The_Great_Sigma
The_Great_Sigma

Reputation: 13

Bgcolor not working in while loop

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.

Upvotes: 0

Views: 93

Answers (3)

AstroCB
AstroCB

Reputation: 12367

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.

Upvotes: 0

sir4ju1
sir4ju1

Reputation: 543

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"

Upvotes: 0

Hozikimaru
Hozikimaru

Reputation: 1156

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

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

Upvotes: 1

Related Questions