This example describes the way JS opens a new window to prevent blocked browsers. Share to everyone for your reference. The specific analysis is as follows:
The traditional window.open () way to open a new window, will be blocked by the browser, then, how can we let JS open a new window without being blocked by the browser? In fact, the solution is still there, here we will analyze how to solve this problem
I've been having this problem lately, so I'm going to share it with the way you pop up the new window. Welcome everyone to add Oh ...
The first type of window.open () method that uses native JavaScript (most of which will be browsed to self blocking)
The second, analog form (form) submission is to specify that the action of the form is the URL address that you want to open, and target is set to "_blank"
Copy Code code as follows:
document.getElementById ("MsgTxt"). Innerhtml= "<form id= ' hiddenlink ' action= '" +shref+ "' target= ' _blank ' >< Input type= ' hidden ' name= ' object ' value= ' +objvalue+ ' ' ></form> ';
var S=document.getelementbyid ("Hiddenlink");
S.submit ();
However, the method of simulating form submission may also be blocked ...
Third, analog hyperlink (<a>) is clicked
When you press a button, you want to open a new tab, you can simulate the link being pressed, and then open the link.
However, in jquery, using A.click (), A.trigger (' click '), etc. will not cause the link default event to be executed.
The following code simulation generates a link-click event and then executes the event that opens the link by default.
However, it is worth noting that: corresponding to IE browser, only IE9 above support the Document.createevent function, so the following code in the execution of IE to IE9 above the line
Copy Code code as follows:
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, using browser bubbling events (reproduced)
Copy Code code 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);
A.click (function (e) {
if (returndata) {
A.attr ("href", returndata);
}else {
A.before (Me);
E.stop ();
}
});
}
Me.mouseover (function () {$ (this). before (a). Appendto (a);});
Me.mouseout (function () {A.before ($ (this));};
Me.click (function () {
A.attr ("href", "#|");
Returndata = F.apply (this, arguments);
});
}
1. First of all, say the final effect, is to implement the "A" contains the elements you want to trigger the window, the original Click event to return to the window of the URL corresponding to this sentence:
Copy Code code as follows:
Returndata = F.apply (this, arguments);
2. Then we will talk about the strategy of blocking the window, specifically I will not say, anyway, the strategy will not intercept "A" itself
3. Finally, the synthesis, with a contains, because the event will bubble, so use the normal click, generate a dynamic link address to a, triggering A's original click event, completed.
I hope this article will help you with your JavaScript programming.