Window Object:: Ganesh Dutt
Window Object:: Ganesh Dutt
BOM
1. Window Object:
The window object represents the browser window or tab and provides
methods and properties to manipulate it.
<body>
<button onclick="resizeWindow()">Resize Window</button>
<script>
function resizeWindow() {
// Resize the window to a specific width and height
window.alert (“I am browser object model”);
}
</script>
</body>
Ganesh Dutt
1. alert(message):
javascript
window.alert("This is an alert message!"); // Displays an alert dialog with the
message
2. confirm(message):
javascript
var result = window.confirm("Are you sure you want to proceed?"); // Displays
a confirmation dialog
if (result) {
console.log("User clicked OK");
} else {
console.log("User clicked Cancel");
}
3. prompt(message, default):
javascript
var userInput = window.prompt("Please enter your name:", "John Doe"); //
Displays a prompt dialog
console.log("User entered: " + userInput);
Ganesh Dutt
5. clearTimeout(timeoutID):
javascript
var timeoutID = window.setTimeout(function() {
console.log("This will not be executed.");
}, 2000);
7. clearInterval(intervalID):
javascript
var intervalID = window.setInterval(function() {
console.log("This will not be executed.");
}, 1000);
9. close():
javascript
window.setTimeout(function() {
window.close(); // Closes the current browser window after 3 seconds
}, 3000);
Ganesh Dutt
10. print():
javascript
window.print(); // Opens the print dialog for the current document
2. Navigator Object:
The navigator object provides information about the browser itself, such as its
name, version, and platform.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Navigator Object Example</title>
</head>
<body>
<script>
// Accessing user agent information
console.log("User Agent:", window.navigator.userAgent);
3. Screen Object:
The screen object provides information about the user's screen, such as its
width, height, and color depth.
Here are some of the common properties of the window.screen object:
For example:
javascript
console.log("Screen width:", window.screen.width);
console.log("Screen height:", window.screen.height);
console.log("Available width:", window.screen.availWidth);
console.log("Available height:", window.screen.availHeight);
console.log("Color depth:", window.screen.colorDepth);
console.log("Pixel depth:", window.screen.pixelDepth);
console.log("Orientation:", window.screen.orientation.type);
Ganesh Dutt
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Screen Object Example</title>
</head>
<body>
<script>
// Accessing screen width
console.log("Screen Width:", window.screen.width);
4. History Object:
The history object represents the browsing history of the current window.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>History Object Example</title>
</head>
<body>
<button onclick="goBack()">Go Back</button>
<button onclick="goForward()">Go Forward</button>
<script>
function goBack() {
// Navigate back in history
window.history.back();
}
function goForward() {
// Navigate forward in history
window.history.forward();
Ganesh Dutt
}
</script>
</body>
</html>
5. Location Object:
The location object represents the URL of the current window and provides
methods for navigating to different URLs.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Location Object Example</title>
</head>
<body>
<button onclick="redirectToGoogle()">Redirect to Google</button>
<script>
function redirectToGoogle() {
// Redirect to Google's homepage
Ganesh Dutt
window.location.href = 'https://www.google.com';
}
</script>
</body>
</html>