What is the window object?
Window
is the main JavaScript object root, aka the global object
in a browser,
Also can be treated as the root of the document object model.
You can access it as window
What is the difference between window, screen, and document in Javascript?
In other words, in simple terms, you can use these to control your browser.
Let's actually use the browser's console to control the browser in various ways.
window.alert('hello,world');
The functions and objects prepared in advance in JavaScript are all properties of the window object.
window.document, which is used to retrieve and manipulate HTML data,
and window.location, which stores information such as URLs, are all properties of the window object.
This is because information such as HTML and URLs is information that the browser window has.
// Move to Your_Name Wiki window.location.href('https://en.wikipedia.org/wiki/Your_Name');
Omitting the window object
The window object is handled in a special way in JavaScript,
and the window can be omitted when accessing the window object's properties.
In other words, window.alert can be written simply as alert.
alert('hello,world');
This is a special rule only for the window object, and it is a rule like this.
Here are the main objects in the properties of the "window" object.
- document The document object is mainly used to retrieve and manipulate HTML documents.
- location ---- URL information An object to get or change the URL information of the displayed page.
Example 1- Getting the URL
var url = location.href; document.write(url); //=> Write out the URL of the currently displayed page
Example 2- Change the URL to the assigned value
location.href = "http://test.jp"
- history --- history management The history object manages the history. The main properties are summarized below.
- length : the number of pages in the history 5
- back : Go back to the previous page (function without return value)
- forward : Go to the next page.
For example, the usage of "history.back" ↓
// go back to the previous page history.back();
- navigator --- Browser Metadata
The navigator object is an object used to handle metadata about the browser type and version.
The most commonly used one is "navigator.userAgent".
That's how it is.
This article was adapted from this article in Japanese.
Top comments (0)