GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — JavaScript - Window Object
Selected Reading
JavaScript - Window ObjectThe JavaScript window object represents the browser's window. In JavaScript, a 'window' object is a global object. It contains the various methods and properties that we can use to access and manipulate the current browser window. For example, showing an alert, opening a new window, closing the current window, etc. All the JavaScript global variables are properties of window object. All global functions are methods of the window object. Furthermore, when the browser renders the content in the 'iframe, it creates a separate 'window' object for the browser and each iframe. Here, you will learn to use the 'window' object as a global object and use the properties and methods of the window object. Window Object as a Global ObjectAs 'window' is a global object in the web browser, you can access the global variables, functions, objects, etc., using the window object anywhere in the code. Let's understand it via the example below. ExampleIn the below code, we have defined the 'num' global and local variables inside the function. Also, we have defined the 'car' global object. In the test() function, we access the global num variable's value using the 'window' object. <html>
<body>
<div id = "output1">The value of the global num variable is: </div>
<div id = "output2">The value of the local num variable is: </div>
<div id = "output3">The value of the car object is: </div>
<script>
var num = 100;
const car = {
brand: "Honda",
model: "city",
}
function test() {
let num = 300;
document.getElementById("output1").innerHTML += window.num;
document.getElementById("output2").innerHTML += num;
document.getElementById("output3").innerHTML += car.brand;
}
test();
</script>
</body>
</html>
OutputThe value of the global num variable is: 100 The value of the local num variable is: 300 The value of the car object is: Honda You may also use the 'window' object to make a particular variable global from a particular block. Window Object PropertiesThe 'window' object contains the various properties, returning the status and information about the current window. Below, we have covered all properties of the 'window' object with a description. You may use the 'window' as a reference to access these properties.
Here, we will cover some properties with examples. OuterHeight/OuterWidth Properties of the Window objectThe outerHeight property of the window object returns the window's height, and the outerWidth property of the window object returns the window's width. ExampleIn the code below, we used the outerHeight and outerWidth property to get the dimensions of the window. You can change the size of the window and observe changes in the value of these properties. <html>
<body>
<p id = "output1">The outer width of the window is: </p>
<p id = "output2">The outer height of the window is: </p>
<script>
const outerHeight = window.outerHeight;
const outerWidth = window.outerWidth;
document.getElementById("output1").innerHTML += outerWidth;
document.getElementById("output2").innerHTML += outerHeight;
</script>
</body>
</html>
OutputThe outer width of the window is: 1536 The outer height of the window is: 816 ScreenLeft Property of the Window ObjectThe window screenLeft property returns the left position of the current window. ExampleIn the output of the below code, you can see the left position of the current window in pixels. <html>
<body>
<div id = "output">Your browser window is left by: </div>
<script>
const screenLeft = window.screenLeft;
document.getElementById("output").innerHTML += screenLeft + " px.";
</script>
</body>
</html>
OutputYour browser window is left by: 0 px. Window Object MethodsThe 'window' object also contains methods like properties to manipulate the current browser window. In the below table, we have covered the methods of the 'window' object with their description. You may use 'window' as a reference to access and invoke the below methods to make the code readable.
Here, we will cover some methods with examples. JavaScript window.alert() MethodThe window.alert() method allows you to show the pop-up dialog containing the message, warning, etc. It takes the string text as an argument. ExampleIn the below example, when you click the button, it will invoke the alert_func() function and show the pop-up box at the middle top. <html>
<body>
<button onclick = "alert_func()"> Execute Alert </button>
<script>
function alert_func() {
window.alert("The alert_func funciton is executed!");
}
</script>
</body>
</html>
JavaScript window.open() MethodThe window.open() method is used to open a new window. It takes a URL as an argument, which you need to open in a new window. ExampleIn the below code, we used the window.open() method to open a new window in the browser. You can see that the code opens the home page of the 'tutorialspoint' website in the new window. <html>
<body>
<button onclick = "openWindow()"> Open New Window </button>
<script>
function openWindow() {
window.open("https://www.tutorialspoint.com/");
}
</script>
</body>
</html>
JavaScript window.print() MethodThe window.print() method lets you print the web page. ExampleIn the below code, click the button to print the web page. <html>
<body>
<h2> Hello World! </h2>
<button onclick = "printPage()"> Print Webpage </button>
<script>
function printPage() {
window.print();
}
</script>
</body>
</html>
Advertisements
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |