window.open ()

Source: Internet
Author: User

Use the window.open () method to navigate to a specific URL, or to open a new browser window. This method can receive 4 parameters: the URL to be loaded, the window target, an attribute string, and a Boolean value that indicates whether the new page supersedes the currently loaded page in the browser history. Typically, only the first parameter is passed, and the last parameter is used only if a new window is not opened.

If the second argument is passed for the window.open () method, and the parameter is the name of an existing window or frame, the URL specified by the first parameter is loaded in the window or frame that has that name.

window.open ("http://www.cnblogs.com/yuyujuan/" target= "Topframe");//equivalent to <  href= "http://www.cnblogs.com/yuyujuan/"  target= "Topframe"> </a>

If there is a window or frame called "topframe", the URL will be loaded in the window or frame, otherwise a new window will be created and named "Topframe", in addition, the second parameter can be _self, _parent, _top, _blank.

Pop-up window

If the second argument passed to window.open () is not a window or frame that already exists, then the method creates a new window or a new tab based on the character at the third parameter position, if no third argument is passed in, A new browser window with all default settings will open (or open a new tab). The third parameter is ignored when a new window is not opened.

The third parameter is a comma-delimited set string that indicates which attributes are displayed in the new window, with the following settings:

window.open ("http://www.wrox.com/", "Wroxwindow", "Height=400,width=400,top=10,left=10,resizable=yes");

Running the above code opens a new resizable window with an initial size of 400*400 pixels, and 10 pixels from the screen along and to the left.

The window.open () method returns a reference to a new window that refers to an object similar to other window objects, but we can take more control of it.

var newwin =  window.open ("http://www.wrox.com/", "Wroxwindow", "height=400,width=400,top=10,left=10,resizable= Yes "); Newwin.resizeto (500,500); Newwin.moveto (100,100); Newwin.close ();

The above method is limited to the pop-up window opened via window.open (), some browsers may not allow us to resize or move the location for the main browser window by default, and the main window of the browser cannot be closed without the user's permission, but the popup window can call close () method, the window's reference is still in after the popup window is closed.

Pop-up window masking program

Most browsers have built-in pop-up Blocker, and no built-in program browser, you can also install some of the tools with built-in screening programs, the result is that the user can not see the vast majority of pop-up windows to screen out, so, when the pop-up window is blocked, you should consider two possibilities. If it is a pop-up window that is blocked by the browser's built-in blocker, then window.open () is likely to return null, at which point the returned value can be determined to determine if the popup is masked.

var Wroxwin = window.open ("http://www.baidu.com"), if (Wroxwin = = null) {    alert ("The popup was blocked");}

If it is a browser extension or other program-blocked pop-up window, then window.open () usually throws an error, so to accurately detect whether the pop-up window is masked, you must detect the return value at the same time, the window.open () Calls are encapsulated in a try-catch block:

var blocked = false;try{     var wroxwin = window.open ("http://www.baidu.com");     if (Wroxwin = = null) {          blocked = true;    }} catch (ex) {       blocked = true;} if (blocked) {      

In any case, the code above can detect if the pop-up window opened by the call window.open () is blocked, but detecting whether a pop-up window is masked is only one thing, and it does not prevent the browser from displaying messages related to the blocked pop-up window.

window.open ()

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.