JavaScript learns the Window object of 4:bom

Source: Internet
Author: User
Tags object model

The BOM is also called the browser object model, which provides many objects for accessing the functionality of the browser. BOM lack of specifications, each browser provider and according to their own ideas to expand it, then the browser common objects become the standard of fact, so, the BOM itself is no standard or there is no organization to set standards for it.

This article focuses on the core objects in the BOM: the Window object

The Window object is the core object of the BOM, which represents an instance of the browser. The Window object is at the topmost level of the JavaScript structure (as shown), and the Window object is automatically defined for each open windows.

1 properties and methods for objects

The Window object has a series of properties, which are also objects themselves. Properties and methods under the Window object can be called using the window. property and the window. method () or direct properties and methods (). As an example: Window.alert () and alert () are the same meaning.

Because of the many properties and methods of the Window object, it is not listed here. You can consult the documentation yourself.

2 System dialog box

The browser uses the alert (), confirm (), and Prompt () methods to invoke the System dialog box to display information to the user. The System dialog box does not have a relationship with the Web page displayed in the browser, nor does it contain HTML.

To give an example of a simple input prompt box:

<span style= "FONT-SIZE:18PX;" >var num=prompt (' Please enter the first number ', 0), var num1=prompt (' Enter the second number ', 0), var num2= number (num) +number (NUM1); alert (' Two numbers of the and is: ') + num2); </span>

Of course, there are other prompt boxes, such as calling the system to print, find dialog boxes, and so on, you can also set the initial value of the browser's status bar.

3 new Window

Use the window.open () method to navigate to a specific URL, or to open a new browser window, It can accept four parameters: 1. The name or window target of the url;2 to be loaded; 3. A specific string; 4. A Boolean value that indicates whether the new page supersedes the currently loaded page in the browser record.

Give an example of how to use

<span style= "FONT-SIZE:18PX;" >open (' http://www.baidu.com ', ' Baidu ', ' Width=400,height=400,top=200,left=200,toolbar=yes ');</span>

4 intermittent calls and timeout calls

JavaScript is a single-threaded language, but it allows you to control code execution at specific times by setting the time-out value and the interval values. The former executes the code after the specified time, while the latter is executed once every specified time.

The timeout call needs to use the settimeout () method of the Window object, which accepts two parameters: the code to execute and the number of milliseconds to time out. After the settimeout () method is called, the method returns a numeric ID that represents the time-out call. The ID of this timeout call is the unique identifier of the plan execution code that can be used to cancel the timeout call.

To cancel a time-out call plan that has not yet been executed, you can call the Cleartimeout () method and pass the corresponding timeout call ID as an argument to it.

The intermittent call is similar to a timeout call except that it repeats the code at the specified interval until the intermittent call is canceled or the page is unloaded. The method of setting an intermittent call is setinterval (), which accepts the same parameters as settimeout ().

Cancel the intermittent call method and cancel the timeout call similarly, using the Clearinterval () method. But canceling the intermittent call is much more important than canceling the timeout call because, without intervening, the intermittent call will continue until the page closes.

It is generally considered that the use of timeout calls to simulate intermittent invocation is an optimal mode. In a development environment, it is very rare to use a real intermittent call because the ID needs to be canceled based on the situation, and there may be some problems with synchronization, so intermittent calls are not recommended. Below we use JS to write a 5 second timer:

<span style= "FONT-SIZE:18PX;" >var Num=0;var max=5;function Timer () {    //Declare a timer num++;if (Num==max) {alert (' 5 Seconds to end! ');} Else{settimeout (timer,1000);}} SetTimeout (timer,1000);  Executive Timers </span>

At this point, for the BOM window object is finished, there is no advanced things and skills, are the foundation of things, but this is our only way, lofty high-rise ground up!

JavaScript learns the Window object of 4:bom

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.