Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Js Window in JavaScript

Js Window in JavaScript

window Object in JavaScript

The window object represents the browser window and is the global object in the browser environment. It provides properties and methods to interact with the browser and the web page.


Key Points

  • Every global variable or function becomes a property or method of the window object.

  • You can access window properties directly (without writing window.) because it's the global context.

  • Provides access to browser features like dialogs, timers, location, history, and more.


Commonly Used window Properties & Methods

Property / MethodDescriptionExample
window.alert()Shows an alert dialogwindow.alert("Hello!")
window.confirm()Shows a confirmation dialogwindow.confirm("Are you sure?")
window.prompt()Shows a prompt dialogwindow.prompt("Enter your name")
window.locationAccess current URL and navigationwindow.location.href = "https://example.com"
window.historyAccess browser history (back, forward)window.history.back()
window.setTimeout()Calls a function after a delay (ms)window.setTimeout(() => alert("Hi"), 1000)
window.setInterval()Calls a function repeatedly at intervalswindow.setInterval(() => console.log("Tick"), 1000)
window.documentReference to the DOM document objectwindow.document.getElementById("myId")
window.innerWidthWidth of the browser viewportconsole.log(window.innerWidth)
window.innerHeightHeight of the browser viewportconsole.log(window.innerHeight)
window.open()Opens a new browser window or tabwindow.open("https://example.com")
window.close()Closes the current windowwindow.close()

Example Usage

// Show an alert dialogalert("Welcome to the site!");// Redirect to a new pagewindow.location.href = "https://openai.com";// Log window sizeconsole.log("Width: " + window.innerWidth);console.log("Height: " + window.innerHeight);// Use setTimeout to delay a messagesetTimeout(() => {  alert("This appears after 3 seconds");}, 3000);

Notes

  • window is automatically the global scope in browsers, so you can omit window. when calling many of its functions.

  • However, prefixing with window. is sometimes useful for clarity.


If you want, I can show you examples of interacting with window events like resize or scroll!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql