Recently, when using window.open, a detail problem has been overlooked: window.open opens a new window, but sometimes a new window opens, sometimes opening a new tab. Although the two cases are irrelevant to the general demand, it is important to note the need for a strong distinction. So how does this turn out to be different, from the usage of the window.open method to the different browsers.
1, the use of window.open easy to ignore the details
The window.open method has three parameters:
window.open (URL, [name], [configuration])
which
- URL, which is the URL to open a new page
- Name, which can be used to get the window object by name of the newly opened window
- Configuration for new open windows, such as whether there is a menu bar, scroll bar, long height, etc.
For example, a new method call that opens a retractable window with no menu bar, title bar, or toolbar, but has scroll bars, status bars, and address bars, is as follows:
window.open ("index.html", "NewWindow", "Menubar=0,scrollbars=1, resizable=1,status=1,titlebar=0,toolbar=0, Location=1 ");
The above is just a brief description of the method of window.open, but this method is easy to ignore is that the new open window name can be a custom value, in addition to the following values, and the target property value of hyperlink A is the same
Window Name Value |
Describe |
_blank |
Default, opens the URL of the link in a new window |
_self |
Open the link URL in the current window |
_parent |
Open the link URL in the parent window |
_top |
Open a URL in a top-level window |
FrameName |
Opens the link URL in the specified frame |
2. window.open open a new window or open a new tab page
Whether calling window.open is opening a new window or opening a new tab has nothing to do with it, but there are some requirements that have a strong intention in this regard, so it's important to distinguish between them. Specific open what or according to the specific circumstances to decide, to the conclusion is through my test, if there is incorrect place, please criticize correct.
1, window.open (URL) or window.open (URL, name), where name is _blank
- Standard Browser, ie9+ is new tab open link URL
- Ie6-8 is a new window open link URL
2, window.open (URL, name), where name is a non-_blank 4 other value
This will open the link URL in the specified window or frame.
3, window.open (URL, name, configration)
As long as Configration is configured, all browsers are new window open link URL
3. A window that is opened with the Window.Open method may be intercepted in an alternative scenario
For security reasons, some browsers may prevent window.open from opening a link URL, either as a label or as a window. It may be necessary for the user to make browser settings to allow the new page, so that users can set the browser is very undesirable, especially like e-commerce sites, then there are other alternatives?
The answer, of course, is that URLs that are opened with hyperlinks are not intercepted and can be implemented . The practice is to combine the event manual triggering mechanism. Here is the HTML: Analog link is pressed, open the page in a new tab, do not use window.open (may be intercepted) This blog gives a simple example, this article gives the event distribution mechanism for the standard browser:
html><Head> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"> <title>Title</title> <Scriptsrc= "Jquery.js"></Script> <Scripttype= "Text/javascript"> $(function() { $("#btn"). Click (function() { //var a = $ (' a ') [0]; vara= $("<a href= ' http://www.apple.com ' target= ' _blank ' >Apple</a>"). Get (0); vare=Document.createevent ('mouseevents'); E.initevent ('Click', true, true ); A.dispatchevent (e); }); }); </Script></Head><Body> <inputtype= "button"value= "Go to Apple"ID= "BTN"></Body></HTML>
window.open new open window with newly opened tab