What is the window object?
global object in a browser,
Also can be treated as the root of the document object model.
You can access it as
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.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
and the window can be omitted when accessing the window object's properties.
In other words, window.alert can be written simply as alert.
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.
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.