The DOM Body Object is used to represent the HTML <Body> element. The Body element is accessed by getElementByTagName(). It can also be accessed by using a document.body object.
Object Properties:
- Alink: It is used to sets or return the color of the active link in a Document.
- background: It is used to sets or return the background image for a document.
- bgColor: It is used to sets or return the backgroundColor for the document.
- link: It is used to sets or return the color of the unvisited link in the document.
- text: It is used to sets or return the color of a text in a document.
- vLink: It sets or returns the color of the visited link in a document.
Example 1: In this example, we will use DOM body Object.
HTML
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM Body Object</title>
<style>
body {
text-align: center;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h2>DOM body Object</h2>
<button onclick="myGeeks()">Submit</button>
<p id="sudo"></p>
<script>
function myGeeks() {
let w = document.getElementsByTagName("BODY")[0];
w.style.backgroundColor = "dodgerblue";
}
</script>
</body>
</html>
|
Output:
Example 2: A body Object can be created by using the document.createElement Method.
HTML
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM Body Object</title>
<style>
body {
text-align: center;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h2>DOM body Object</h2>
<button onclick="myGeeks()">Submit</button>
<p id="sudo"></p>
<script>
function myGeeks() {
let w = document.getElementsByTagName("BODY")[0];
w.style.backgroundColor = "dodgerblue";
}
</script>
</body>
</html>
|
Output:
Supported Browsers: The browser supported by DOM Body Object are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari