JavaScript getting started tutorial (6) Window objects

Source: Internet
Author: User

Window objects in JavaScript
It is the largest object in JavaScript and describes a browser window. Generally, to reference its attributes and methods, you do not need to use the "window. xxx" format, but directly use "xxx ". A framework page is also a window.
Window objects have the following attributes:
Name of the window, which is connected by opening the window (<a target = "... ">) or frame page (<frame name = "... ">) or the open () method called by a window (see below. This attribute is generally not used.
Status indicates the content displayed in the "status bar" under the window. By assigning values to status, you can change the display of the status bar.
Opener usage: window. opener; return the window object that opens the window. Note: A window object is returned. If the window is not opened by another window, null is returned for this attribute in Netscape; "undefined" is returned for IE ). Undefined is equal to null to some extent. Note: undefined is not a JavaScript constant. If you attempt to use "undefined", "undefined" is returned.
Self refers to the window itself, which returns the same object as the window object. The most common is "self. close () ", placed in the <a> tag:" <a href = "javascript: self. close () "> close Window </a> ".
Parent returns the frame Page Object of the window.
Top returns the framework page Objects occupying the top of the entire browser window.
History object, see.
Location address object, see.
Document Object, see.
The Window object has the following methods:
The first method is open () to open a window.
Usage:
Copy codeThe Code is as follows:
Open (<URL string>, <window name string>, <parameter string> );

Note:
<URL string>: Describe the webpage opened in the window. If you leave it blank (''), no webpage is opened.
<Window name string>: the name of the window to be opened (window. name). You can use built-in names such as '_ top' and '_ blank. The name here is the same as the "target" attribute in "<a href ="... "target ="... ">.
<Parameter string>: Describes the appearance of the window to be opened. If you only need to open a normal window, leave this string blank (''). If you want to specify the appearance, write one or more parameters in the string, separated by commas.
For example, open a clean window of 400x100:
Copy codeThe Code is as follows:
Open ('', '_ blank', 'width = 400, height = 100, menubar = no, toolbar = no,
Location = no, directories = no, status = no, scrollbars = yes, resizable = yes ')

Open () Parameters
Top = # number of records that exit from the top of the screen at the top of the window
Left = # number of records left when the left side of the window leaves the screen
Width = # window width
Height = # window height
Menubar =... Is there any menu in the window? The value is yes or no.
Toolbar =... does the window have a toolbar? The value is yes or no.
Location =... Is there an address bar in the window? The value is yes or no.
Directories =... whether there is a connection area in the window. The value is yes or no.
Scrollbars =... does the window have a scroll bar? The value is yes or no.
Status =... does the window have a status bar? values: yes or no
Resizable =... the window is not adjusted. The value is yes or no.
Note: The open () method has a return value, which is the window object it opens. For example
Var newWindow = open ('', '_ blank ');
In this way, a new window is assigned to the "newWindow" variable, and the window can be controlled through the "newWindow" variable.
Close () closes an opened window.
Usage:
Copy codeThe Code is as follows:
Window. close ()

Or
Copy codeThe Code is as follows:
Self. close ()

The main function is to close this window;
<Window Object>. close (): close the specified window. Note: If the window has a status bar, the browser will warn after calling this method: "The webpage is trying to close the window. Are you sure you want to close it ?" Then wait for the user to choose whether or not. If there is no status bar, calling this method will close the window.
In addition, the Window object also has the following method:
Blur () removes the focus from the window and changes the window to an "inactive window ".
Focus () is the focus of the window and changes to "activity window ". However, in Windows 98, this method can only Flash the title bar of the window and the corresponding buttons on the taskbar, prompting the user that the window is trying to get the focus.
ScrollTo () usage: [<window Object>.] scrollTo (x, y); enables the window to scroll from the (x, y) points in the upper left corner of the window to the upper left corner of the window.
ScrollBy () usage: [<window Object>.] scrollBy (deltaX, deltaY); enables the window to scroll to the right of the deltaX pixel, scroll down the deltaY pixel. If a negative value is obtained, it is rolled in the opposite direction.
ResizeTo () usage: [<window Object>.] resizeTo (width, height); enables the window to be adjusted to width, width, and height.
ResizeBy () usage: [<window Object>.] resizeBy (deltaWidth, deltaHeight); adjust the window size, increase the width of deltaWidth pixels, and increase the height of deltaHeight pixels. If the value is negative, the value is reduced.
Alert () usage: alert (<string>); a dialog box containing only the "OK" button is displayed, showing the content of <string>, reading the entire document and running the Script will be paused until the user presses "OK ".
Confirm () usage: confirm (<string>); a dialog box containing the "OK" and "cancel" buttons is displayed. The <string> content must be selected, reading the entire document and running the Script are paused. If you press "OK", true is returned. If you press "cancel", false is returned.
Prompt () usage: prompt (<string> [, <initial value>]); a dialog box containing "OK" and "cancel" and a text box is displayed, showing <string> content, users are required to enter some data in the text box. Reading the entire document and running the Script are paused. If the user presses "OK", the existing content in the text box is returned. If the user presses "cancel", the return value is null. If <initial value> is specified, the default value is displayed in the text box.
Window objects have the following events:
Window. onload; occurs when all documents are downloaded. After all the files are downloaded, not only HTML files, but also images, plug-ins, controls, applets, and other content are downloaded. This event is a window event, but when you specify an event handler in HTML, we write it in the <body> tag.
Window. onunload; occurs when the user exits the document (or closes the window, or goes to another page. Like onload, it should be written in HTML and written into the <body> tag.
Window. onresize; occurs when the window is adjusted.
Window. onblur; occurs when the window loses focus.
Window. onfocus; occurs when the window gets the focus.
Window. onerror; occurs when an error occurs. Its event Handler is usually called an Error Handler to handle errors. As described above, to ignore all errors, use:
Copy codeThe Code is as follows:
Function ignoreError (){
Return true;
}
Window. onerror = ignoreError;

Related Article

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.