JS open a new window to prevent the method from being blocked by the browser. js new window
This example describes how to open a new window in JavaScript to prevent being blocked by the browser. Share it with you for your reference. The specific analysis is as follows:
Opening a new window in the traditional window. open () mode will be blocked by the browser. So how can we prevent JS from opening a new window by the browser? There are still some solutions. Here we will analyze how to solve this problem.
I recently encountered such a problem, so I will share the method of the pop-up window with you. Thank you for your attention...
First, use the window. open () method of native javascript (in most cases, it will be browsed and self-blocked)
Second, simulate form submission. The principle is to specify the form action as the URL to be opened, and set target to "_ blank"
Copy codeThe Code is as follows: document. getElementById ("msgTxt "). innerHTML = "<form id = 'dendenlink' action = '" + sHref + "'target =' _ blank '> <input type = 'den den 'name = 'object' value = '"+ objValue +"'> </form> ";
Var s = document. getElementById ("hiddenlink ");
S. submit ();
However, the method for simulating form submission may also be blocked...
Third, the simulated hyperlink (<a>) is clicked
When you press a button to open a new tab, you can simulate the link being pressed and then open the link.
However, in jQuery, the use of a. click (), a. trigger ('click') and so on will not cause the default link event to be executed.
The following code simulates the link click event and then executes the event that opens the link by default.
However, it is worth noting that only IE9 and later support the document. createEvent function for IE browser, so the following code must be executed by IE9 and later.
Copy codeCode: var a = $ ("<a href = 'HTTP: // www.test.com 'target =' _ blank '> test </a>"). get (0 );
Var e = document. createEvent ('mouseevents ');
E. initEvent ('click', true, true );
A. dispatchEvent (e );
Fourth, the use of browser bubble events (reprinted)
Copy codeThe Code is as follows: clickOpenWin: function (f ){
Var dataKey = "clickOpenWin. dataKey"
Var me = $ (this );
Var A = me. data (dataKey );
Var returnData = null;
If (! A ){
A = $ ("");
Me. data (dataKey, );
A. click (function (e ){
If (returnData ){
A. attr ("href", returnData );
} Else {
A. before (me );
E. stop ();
}
});
}
Me. mouseover (function () {$ (this). before (A). appendTo ();});
Me. mouseout (function () {A. before ($ (this ));});
Me. click (function (){
A. attr ("href", "# | ");
ReturnData = f. apply (this, arguments );
});
}
1) First, let's talk about the final effect, which is to use "A" to include the elements you want to trigger the pop-up window. The original click event will return the corresponding URL of the pop-up window:Copy codeThe Code is as follows: returnData = f. apply (this, arguments );
2). Next, let's talk about the pop-up window interception policy. I will not talk about it. It will not intercept "A" itself in the Policy anyway.
3 ). finally, it is merged. After A is included, the event will bubble up, so the normal click is used to generate A dynamic link address for A and trigger the original Click Event of.
I hope this article will help you design javascript programs.