我正在尝试在新标签页中打开一个URL,而不是弹出窗口。
我看到了类似的问题,其响应大致如下:
window.open(url,'_blank');
window.open(url);
但是这些方法都没有对我起作用,浏览器仍然试图打开一个弹出窗口。
我正在尝试在新标签页中打开一个URL,而不是弹出窗口。
我看到了类似的问题,其响应大致如下:
window.open(url,'_blank');
window.open(url);
但是这些方法都没有对我起作用,浏览器仍然试图打开一个弹出窗口。
window.open()
的行为取决于其使用方式。如果它作为用户操作(例如按钮单击)的直接结果被调用,它应该能够正常工作并打开一个新的标签页(或窗口):const button = document.querySelector('#openTab');
// add click event listener
button.addEventListener('click', () => {
// open a new tab
const tab = window.open('https://attacomsian.com', '_blank');
});
然而,如果您尝试从AJAX请求回调中打开一个新标签页,则浏览器将阻止该操作,因为它不是直接用户动作。
要绕过弹出窗口拦截程序并在回调中打开一个新标签页,可以使用以下小技巧:
const button = document.querySelector('#openTab');
// add click event listener
button.addEventListener('click', () => {
// open an empty window
const tab = window.open('about:blank');
// make an API call
fetch('/api/validate')
.then(res => res.json())
.then(json => {
// TODO: do something with JSON response
// update the actual URL
tab.location = 'https://attacomsian.com';
tab.focus();
})
.catch(err => {
// close the empty window
tab.close();
});
});
如果省略strWindowFeatures
参数,将会打开一个新的标签页,除非浏览器设置覆盖它(浏览器设置胜过JavaScript)。
新窗口
var myWin = window.open(strUrl, strWindowName, [strWindowFeatures]);
新标签页
var myWin = window.open(strUrl, strWindowName);
-- 或者 --
var myWin = window.open(strUrl);
如果您正试图从自定义函数中打开新选项卡,则与浏览器设置无关。
在此页面中,打开JavaScript控制台并键入:
document.getElementById("nav-questions").setAttribute("target", "_blank");
document.getElementById("nav-questions").click();
无论您的设置如何,它都将尝试打开弹出窗口,因为“click”来自自定义操作。
为了像实际的链接点击一样运行,您需要遵循spirinvladimir的建议并且真正地创建它:
document.getElementById("nav-questions").setAttribute("target", "_blank");
document.getElementById("nav-questions").dispatchEvent((function(e){
e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,
false, false, false, false, 0, null);
return e
}(document.createEvent('MouseEvents'))));
这是一个完整的示例(不要在jsFiddle或类似的在线编辑器上尝试,因为它不会允许你从那里重定向到外部页面):
<!DOCTYPE html>
<html>
<head>
<style>
#firing_div {
margin-top: 15px;
width: 250px;
border: 1px solid blue;
text-align: center;
}
</style>
</head>
<body>
<a id="my_link" href="http://www.google.com"> Go to Google </a>
<div id="firing_div"> Click me to trigger custom click </div>
</body>
<script>
function fire_custom_click() {
alert("firing click!");
document.getElementById("my_link").dispatchEvent((function(e){
e.initMouseEvent("click", true, true, window, /* type, canBubble, cancelable, view */
0, 0, 0, 0, 0, /* detail, screenX, screenY, clientX, clientY */
false, false, false, false, /* ctrlKey, altKey, shiftKey, metaKey */
0, null); /* button, relatedTarget */
return e
}(document.createEvent('MouseEvents'))));
}
document.getElementById("firing_div").onclick = fire_custom_click;
</script>
</html>
w = window.open("placeholder"); $.ajax(...ajax调用返回url...) success (...w.location = data.url...)
,参考 http://theandystratton.com/2012/how-to-bypass-google-chromes-javascript-popup-blocker。 - Paul Tomblin(function(a) {
document.body.appendChild(a);
a.setAttribute('href', location.href);
a.dispatchEvent((function(e) {
e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, false, 0, null);
return e
}(document.createEvent('MouseEvents'))))
}(document.createElement('a')))
function openTab(url) {
const link = document.createElement('a');
link.href = url;
link.target = '_blank';
document.body.appendChild(link);
link.click();
link.remove();
}
你可以使用一个关于form
的技巧:
$(function () {
$('#btn').click(function () {
openNewTab("http://stackoverflow.com")
return false;
});
});
function openNewTab(link) {
var frm = $('<form method="get" action="' + link + '" target="_blank"></form>')
$("body").append(frm);
frm.submit().remove();
}
a
ж ҮзӯҫиҖҢдёҚжҳҜиЎЁеҚ•пјҢ并дҪҝз”ЁjQueryзҡ„.click()
ж–№жі•жқҘвҖңжҸҗдәӨвҖқгҖӮиҜ·жҹҘзңӢжҲ‘зҡ„зӯ”жЎҲгҖӮ - user3186555不要使用target="_blank"
始终为该窗口使用特定的名称,在我的情况下是meaningfulName
。这样可以节省处理器资源:
button.addEventListener('click', () => {
window.open('https://google.com', 'meaningfulName')
})
这样,当您点击一个按钮10次时,浏览器将始终在一个新标签页中重新渲染它,而不是在10个不同的标签页中打开它,这将消耗更多的资源。
您可以在MDN上了解更多相关信息。
$('<a />',{'href': url, 'target': '_blank'}).get(0).click();
Object.assign(document.createElement('a'), { target: '_blank', href: 'URL_HERE'}).click();
要在新标签页中打开并保持在同一位置,您可以在新标签页中打开当前页面,并将旧标签页重定向到新的URL。
let newUrl = 'http://example.com';
let currentUrl = window.location.href;
window.open(currentUrl , '_blank'); // Open window with the URL of the current page
location.href = newUrl; // Redirect the previous window to the new URL
var evLink = document.createElement('a');
evLink.href = 'http://' + strUrl;
evLink.target = '_blank';
document.body.appendChild(evLink);
evLink.click();
// Now delete it
evLink.parentNode.removeChild(evLink);
这不应该被任何弹出窗口拦截器阻止...希望如此。