Recently in the project encountered the use of window.open by the browser to intercept the situation, in the local experiment is not a problem, to the server was intercepted, Firefox has interception prompts, 360 browser blocking hints are not, although in their own environment can be released to the page, but for users, not to require users to intercept. Not to mention when the interception, a lot of small white at all do not know what happened, do not know where to see the blocked page, baffled its solution, later checked, the browser support is not the same.
In addition, it can be found that when the window.open for the user to trigger the event inside or load, will not be intercepted, once the pop-up code moved to Ajax or a piece of asynchronous code inside, immediately appear to be intercepted performance .
Use a customizable pop-up window's look, size, pop-up position to fit the page's window.open () method, as shown in the code below:
[HTML]View PlainCopy print?
- <script language= "java script:>
- <!--
- window.open (' page.html ', ' newwindow ', 'height= ',width=, top=0,left=0, Toolbar=No,menubar=No,scrollbars=No,resizable=No,
- location=No,status=no ')
- Write a line
- -->
- </SCRIPT>
[HTML]View PlainCopyprint?
- Parameter explanation:
- window.open the command that pops up the new window;
- page.html the file name of the new window pops up;
- NewWindow the name of the popup window (not the file name), can be replaced by empty ″;
- height=100 window height;
- top=The pixel value of the 0 window from the top of the screen;
- left=0 The pixel value of the window from the left side of the screen;
- Toolbar=No whether the toolbar is displayed, yes is displayed;
- Menubar,scrollbars represents the menu bar and scroll bar;
- resizable=No whether the window size is allowed to change, yes is allowed;
- location=No whether the address bar is displayed, yes is allowed;
- status=No whether the information in the status bar is displayed (usually the file is already open), yes is allowed;
Cause analysis
When the browser detects a new pop-up window created by a non-user operation, it is blocked. Because the browser thinks it might be an ad, it's not a page that a user wants to see.
Solution: 1, use a tag instead
The following function, which binds the function to the click event callback, avoids most browsers blocking the popup:
[HTML]View PlainCopyprint?
- function Newwin (URL, id) {
- var a = document.createelement (' a ');
- A.setattribute (' href ', url);
- A.setattribute (' target ', ' _blank ');
- A.setattribute (' ID ', id);
- Prevent repeated additions
- if (!document.getelementbyid (ID)) {
- Document.body.appendChild (a);
- }
- A.click ();
- }
2. Use form's Submit method to open a page
This method needs to construct a from and then trigger the form's submit by the JS code, submitting the form to a new page, such as:
[HTML]View PlainCopyprint?
- $ ("#tijiaos"). attr (' target ', ' _blank ');
- $ ("#tijiaos"). Submit ();
Note that the above two methods do not fit in the Ajax callback function, if placed in the callback function, will still be blocked by the browser.
3. The ultimate solution – pop-up the window first, then redirect
The third scenario, in fact, is a workaround, the core idea is: first click on the user to open the page, and then redirect the page . The sample code is as follows:
[HTML]View PlainCopyprint?
- Xx.addeventlistener (' click ', function () {
- Open the page, it is best to use the prompt page
- var newwin = window.open (' loading page ');
- Ajax (). Done (function () {
- Redirect to target page
- newWin.location.href = ' target URL ';
- });
- });
The above method is actually open two address, so we recommend that you open the first address when giving a similar ' current page is loading, please later. ' Simple Tip page, so you can avoid opening two real target pages, allowing users to perceive the redirection of the page .
Add:
JS 2 ways to open a new window
1. Hyperlink <ahref= "Http://www.jb51.NET" title= "Scripting House" >Welcome</a>
Equivalent to JS code
Window.location.href= "Http://www.jb51.Net"; Open a window in the same current window
2. Hyperlink <ahref= "Http://www.jb51.net" title= "Script House" target= "_blank" >Welcome</a>
Equivalent to JS code
window.open ("Http://www.jb51.net"); Open a window in another new window
3. Close the new window:
This.window.opener =null;
Window.close ();
window.open Browser Popup New window blocked-cause analysis and solutions