window.open(url1, "name1", params); // this open one window or tab
window.open(url1, "name2", params); // though url is same, but it'll open in another window(tab).
// this function can fire onclick handler for any DOM-Element
function fireClickEvent(element) {
var evt = new window.MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true
});
element.dispatchEvent(evt);
}
// this function will setup a virtual anchor element
// and fire click handler to open new URL in the same room
// it works better than location.href=something or location.reload()
function openNewURLInTheSameWindow(targetURL) {
var a = document.createElement('a');
a.href = targetURL;
fireClickEvent(a);
}
上面的代码也有助于打开新的标签/窗口,绕过所有弹出窗口拦截器!!如。
function openNewTabOrNewWindow(targetURL) {
var a = document.createElement('a');
a.href = targetURL;
a.target = '_blank'; // now it will open new tab/window and bypass any popup blocker!
fireClickEvent(a);
}
const autoOpenAlink = (url = ``) => {
window.open(url, "open testing page in a same tab page");
}
<a
href="https://cdn.xgqfrms.xyz/index.html"
target="_self"
onclick="autoOpenAlink('https://cdn.xgqfrms.xyz/index.html')">
open url in the current tab page using `_self`
</a>
在新标签页中使用_blank打开url
const autoOpenAlink = (url = ``) => {
window.open(url, "open testing page in a new tab page");
}
// ❌ The error is caused by a `StackOverflow` limitation
// js:18 Blocked opening 'https://cdn.xgqfrms.xyz/index.html' in a new window because the request was made in a sandboxed frame whose 'allow-popups' permission is not set.
<a
href="https://cdn.xgqfrms.xyz/index.html"
target="_blank"
onclick="autoOpenAlink('https://cdn.xgqfrms.xyz/index.html')">
open url in a new tab page using `_blank`
</a>