One of the JavaScript browser objects, Window object explanation, javascriptwindow

Source: Internet
Author: User

One of the JavaScript browser objects, Window object explanation, javascriptwindow

JavaScript provides a set of window-oriented objects to implement browser window access control. JavaScript defines six important objects:

Window object indicates the window opened in the browser;

The document Object indicates the document object that loads the page in the browser;

The location object contains the current URL Information of the browser;

The navigation object contains information about the browser;

The screen object contains information about the client screen and rendering capabilities;

The history object contains the historical information about the web page accessed by the browser.

In addition to the window object, the other five objects are the properties of the window object. Their relationships are as follows:

1. window object

The window object is the Global Object of JavaScript. Therefore, you do not need to specify the properties and methods of the window object. For example, alert, the complete call is actually window. alert, which usually ignores the reference of the window object.

1. properties, methods, and events of windows objects

Properties of the window object
Attribute name Function Demo
Name Window name
Parent The parent window of the current window (framework). It is used to return the methods and properties of the object.
Opener Return the window object that generates the current window. Use it to return the methods and properties of the object.
Top Indicates the main window, which is the top-level window and the parent window of all other windows. You can use this object to access the methods and properties of the current window.
Self Returns an object of the current window. You can use this object to access the methods and properties of the current window.
Defaultstatus Return or set the default content that will be displayed in the browser Status Bar
Status Returns or sets the specified content that will be displayed in the browser status bar.

Window object Method
Method Name
Function
Demo
Alert ()
Displays an alert dialog box that contains a message and a confirm button.

Confirm ()
Show a confirmation dialog box

Prompt ()
A prompt dialog box is displayed, prompting users to enter data.

Open ()
Open an existing window, or create a new window, and load a document in the window

Close ()
Close an open window

Navigate ()
Display the specified webpage in the current window

SetTimeout ()
Sets a timer and calls a function after a specified interval.

ClearTimeout ()
Resets a specified timer.

Focus ()
Make a Window object get the current focus

Blur ()
Causes a Window object to lose the current focus

Window object event
Event
Description
Demo
Onload
HTML file loading occurs in the browser

Onunload
HTML file deleted from the browser

Onfocus
Occurs when the window gets focus

Onblur
Window focus loss occurs

Onhelp
The user presses the F1 key.

Onresize
The window size is adjusted by the user.

Onscroll
An error occurred while rolling the window.

Onerror
Error loading HTML file


2. Main functions provided by the window object:

Adjust the size and position of the window, open a new window, system prompt box, status bar control, and scheduled operation. The five functions are described in detail below.

(1) Adjust the size and position of the window

Method

Usage

Description

Window. moveBy

Move the browser window to a specified position (relative location)

Window. moveBy (dx, dy)

For the sake of security and good consideration for users, you must always ensure that the browser window is in the visible area of the screen Because JavaScript scripts are not allowed to move the window outside the visible area.

Window. moveTo

Move the browser window to the specified position

(Absolute positioning)

Window. moveBy (x, y)

If the specified coordinates (x, y) place some or all windows outside the visible area, the window will stay closest to the edge of the screen.

Window. resizeBy

Change the size of the browser window to the specified width and height (relatively adjust the window size)

Window. resizeBy (dw, dh)

Window. resizeTo

Change the size of the browser window to the specified width and height (absolutely adjust the window size)

Window. resizeTo (w, h)

The specified width and height cannot be negative.

(2) open a new window

Usage: window. open ([url], [target], [options])

Parameter url: the url to be loaded in the new window. If no parameter is specified, a blank page is loaded by default. For example, window. open ("test.htm ");

Target: the target or name of the newly opened window.

_ Self: load a new page in the current window

_ Blank: loading a new page in a new window

_ Parent load a new page in the parent window

_ Top load a new page in the top-level window

Parameter options: the properties of the newly opened window. The options are separated by commas (,). Each option contains the option name and value.

Option

Description

Height

The height of the window, in pixels.

Width

The window width, in pixels.

Left

Left edge position of the window

Top

Upper edge position of the window

Fullscreen

Full Screen or not. The default value is no.

Location

Whether to display the address bar. The default value is yes.

Menubar

Whether to display the menu bar. The default value is yes.

Resizable

Whether to change the window size. The default value is yes.

Scrollbars

Whether to display the scroll bar. The default value is yes.

Status

Whether to display the status bar. The default value is yes.

Titlebar

Whether to display the title bar. The default value is yes.

Toolbar

Whether to display toolbar. Default Value: yes

(3) system prompt box

Window. alert: displays the Message prompt box. Use window. alert ([Message]). (Note: Generally, the window object is omitted and alert is used directly)

Window. confirm displays a confirmation dialog box, including the "OK" and "cancel" buttons.

Window. prompt displays a message prompt box, which contains a text input box.
Usage: window. prompt ([Message], [default]); Message is the text displayed in the prompt box, and default is the default value of the text box.

(4) Status Bar Control

Use the window. status attribute for control. For example: window. status = "error prompt"; this will affect the user experience, so it is not recommended to modify the status bar information.

(5) scheduled operation

Scheduled operations are common functions in web development. In Ajax-based development, an application needs to regularly access the backend server and update the front-end page, such application implementations usually rely on scheduled operation functions.

There are four scheduled operation functions: window. setInterval, window. clearInterval, window. setTimeout, window. clearTimeout, these four functions are the window object method, which indicates that the browser's scheduled operations are completed by the browser window. The usage of these four methods is described in detail below.

① Window. setInterval: set the timer and run the specified code window. setInterval (code, time) at intervals );

Note: The code parameter can be a function or a string form of JavaScript code.

The time parameter is the time interval between code execution. The unit is ms.

② Window. clearInterval clear the timer window. clearInterval (time) set by the setInterval function );

③ Window. setTimeout: set the timer and run the specified code window. setTimeout (code, time) at intervals );

Note: The code parameter can be a function or a string-type JavaScript code. The difference between setTimeout and setInterval is that setTimeout only executes the specified code once.

The time parameter is the time interval between code execution. The unit is ms.

④ Window. clearTimeout clears the timer window. clearTimeout (time) set by the setTiimeout function );

One of the above JavaScript browser objects, Window objects, is all the content shared by the editor. I hope to give you a reference, and I hope you can provide more support for the help house.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.