This article describes how JS opens a new window to prevent it from being blocked by the browser. Share it for your reference. The specific analysis is as follows:
Opening a new window using the traditional window.open() method will be blocked by the browser. So, how can we make JS open a new window without being blocked by the browser? Actually, there are still solutions, here we will analyze how to solve this problem
I have encountered such a problem recently, so I will share with you the method of popping up a new window. Everyone is welcome to add...
The first is to use the native javascript window.open() method (in most cases, it will be blocked by browsing)
The second type is to simulate form submission. The principle is to specify that the action of the form is the URL address you want to open, and the target is set to "_blank"
The code copy is 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 mock form submission may also be blocked...
The third type, simulated hyperlink (<a>) is clicked
When a button is pressed, if 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') and so on will not cause the link default event to be executed.
The following code simulates the link click event, and then executes the event where the link is opened by default.
However, it is worth noting that: for IE browser, the document.createEvent function is supported only at IE9 or above, so the following code must be executed in IE9 or above.
The code copy is 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);
The fourth type: bubbling incident using the browser (reprinted)
The code copy 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);
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, let’s talk about the final effect, which is to use “A” to contain the elements you want to trigger the pop-up window. The original click event will return to the pop-up URL. Corresponding to this sentence: Copy the code code as follows: returnData = f.apply(this, arguments);
2). Then we will talk about the pop-up blocking strategy. I won’t talk about the specific ones. Anyway, the strategy will not intercept "A" itself.
3). Finally, it is synthesized. After using A to include it, because the event will bubble, use normal clicks to generate a dynamic link address to A, triggering the original click event of A, and then it is completed.
I hope this article will be helpful to everyone's JavaScript programming.