我通过window.location
将用户重定向到某个url,但是该url在浏览器的同一标签页中打开。我想让它在新标签页中打开。我能否使用window.location实现这一点?还有其他方法可以完成此操作吗?
我通过window.location
将用户重定向到某个url,但是该url在浏览器的同一标签页中打开。我想让它在新标签页中打开。我能否使用window.location实现这一点?还有其他方法可以完成此操作吗?
window.open('https://support.wwf.org.uk', '_blank');
第二个参数是使其在新窗口中打开的原因。别忘了阅读Jakob Nielsen的信息文章 :)
window.open('https://support.wwf.org.uk', "_blank") || window.location.replace('https://support.wwf.org.uk');
如果弹窗被阻止,这将在同一标签页打开它。
我认为除非你编写浏览器扩展程序,否则没有办法做到这一点。你可以尝试使用 window.open
方法,并希望用户的浏览器设置为在新标签页中打开新窗口。
window.open(url, '_blank');
- undefined这对我在 Chrome 53 上有效。在其他地方没有测试过:
function navigate(href, newTab) {
var a = document.createElement('a');
a.href = href;
if (newTab) {
a.setAttribute('target', '_blank');
}
a.click();
}
使用jQuery会更加容易,并且在Chrome上也可以工作
$('#your-button').on('click', function(){
$('<a href="https://www.some-page.com" target="blank"></a>')[0].click();
})
相比于使用弹出窗口,我个人更喜欢这种解决方案,它在这个问题的讨论串中被提到: JavaScript:location.href打开新的窗口/选项卡?
$(document).on('click','span.external-link',function(){
var t = $(this),
URL = t.attr('data-href');
$('<a href="'+ URL +'" target="_blank">External Link</a>')[0].click();
});
这是一个关于it技术的 示例。
let url = 'yourUrl?Param=' + Param;
window.open(url, '_blank');
我们需要动态设置属性target="_blank",这样它就会在新标签页中打开。
document.getElementsByTagName("a")[0].setAttribute('target', '_blank')
document.getElementsByTagName("a")[0].click()
如果想在新窗口打开,获取 href 链接并使用 window.open
var link = document.getElementsByTagName("a")[0].getAttribute("href");
window.open(url, "","height=500,width=500");
请勿在上述代码中提供第二个参数作为 _blank。
window.location
是必须的吗?或者其他 JS 解决方案可行吗? - Khez