Table of Contents
Performing Basic Window function in JavaScript
The Window object is supported by all the browsers and is used to represent a browser’s window. We can perform various functions using the Window Objects. Following are the widely used Window functions with their description:
Window Function | Description | Example |
window.alert( ) | This method displays an alert box with your message and OK button. | window.alert(“Hello World”); |
window.confirm( ) | This method displays a dialog box with an optional message and two buttons i.e. OK and Cancel. | if (window.confirm(“Do you want to exit?”)) {window.alert(“Thanks for Visiting!”); } |
window.prompt( ) | This method opens a dialog box that prompts a user for input. | prompt(“What’s your name?”); |
window.print( ) | This method prints the current screen. | window.print( ); |
window.open( ) | This method is used to open a new browser window or a new tab. This is mainly used to open a link in a new tab. | window.open(“https://gocoding.org”); |
window.close( ) | This method closes the current window. This only works if the window was opened using window.open(). | newWindow = window.open(“”, “newWindow”, “width=200, height=100”); newWindow.close(); |
window.history.back( ) | This method loads the previous URL from the history list. | window.history.back(); |
window.history.forward( ) | This method loads the next URL from the history list. | window.history.forward(); |
window.localStorage( ) | This method is used to store & retrieve data (key/ value pair) from the browser’s local storage. | window.localStorage.setItem(‘myName’, ‘Rudra’); window.localStorage.getItem(‘myName’); |
window.sessionStorage( ) | This is same as the above function, the difference is that the data gets deleted once the session is closed (i.e. browser’s tab is closed) | window.sessionStorage.setItem(‘myName’, ‘Rudra’); window.sessionStorage.getItem(‘myName’); |
*Note: Please try the above examples in your browser’s console.
0 Comments