Modify the JavaScript property document.body.style.background.
For example:
function changeBackground(color) {
document.body.style.background = color;
}
window.addEventListener("load",function() { changeBackground('red') });
Note: this does depend a bit on how your page is put together, for example if you're using a DIV container with a different background colour you will need to modify the background colour of that instead of the document body.
Answer from user7094 on Stack OverflowModify the JavaScript property document.body.style.background.
For example:
function changeBackground(color) {
document.body.style.background = color;
}
window.addEventListener("load",function() { changeBackground('red') });
Note: this does depend a bit on how your page is put together, for example if you're using a DIV container with a different background colour you will need to modify the background colour of that instead of the document body.
You don't need AJAX for this, just some plain JavaScript setting the background-color property of the body element, like this:
document.body.style.backgroundColor = "#AA0000";
If you want to do it as if it was initiated by the server, you would have to poll the server and then change the color accordingly.
Using JavaScript to change an element's background color via CSS
How to change background color using JavaScript
Need help with changing the background color using javascript
Change background color by DOM manipulation
Videos
I keep getting an error telling me that newColor is undefined, which I thought thats what I did in the Javascript. I'm just lost. Any help would be appreciated.
HTML
<!DOCTYPE html>
<html> <head> <title>Background Color Change</title> <link rel="stylesheet" href="backgroundColor.css"> </head> <body id="body"> <button type="button" onclick="newColor()" id="colorChange"><b>Click Me</b></button> <script type="text/javascript" href="backgroundColor.js"></script> </body> </html>
Javascript
function newColor(){
var changeColor= document.getElementById("body");
changeColor.style.backgroundColor('green');
}
CSS
#colorChange:hover{
background-color: red;
}
#colorChange{
background-color: green;
}
#body{
background-color:blue;
text-align:center;
}