简体   繁体   English

JavaScript使用一个链接打开两个网页

[英]JavaScript to open two web pages using one link

I need JavaScript code or HTML to make two websites open in two new browser tabs when clicking on one link. 单击一个链接时,我需要JavaScript代码或HTML才能在两个新的浏览器选项卡中打开两个网站。 I do not want them to open in new windows, or on the current page that the link is on. 我不希望它们在新窗口或链接所在的当前页面上打开。

It probably won't work because the browser might consider it a popup and block it. 它可能不起作用,因为浏览器可能会认为它是弹出窗口并阻止了它。

If the user allows popups you can do: 如果用户允许弹出窗口,则可以执行以下操作:

window.open(url, '_blank');

Like: 喜欢:

<a id="mydoublelink" href="http://site1.com" target="_blank">foo</a>

document.getElementById("mydoublelink").onclick=function(){
    window.open('http://site2.com', '_blank');
}

If you call window.open in the onclick event you should be fine. 如果您在onclick事件中调用window.open ,就可以了。 Built-in popup blockers allow those. 内置的弹出窗口阻止程序允许这些操作。 The kind of popups that get blocked come from other events or from scheduled events like a setTimeout . 阻止的弹出窗口类型来自其他事件或诸如setTimeout类的预定事件。

document.getElement("my_link").onclick = function () {
    window.open(/*..*/); // works
}

document.getElement("my_link").onclick = function () {
    setTimeout(function () {
        window.open(/*..*/); // will probably get blocked
    });
}

This means, for instance, that if you open a popup after an AJAX call it will very likely get blocked. 例如,这意味着,如果在AJAX调用后打开弹出窗口,则很有可能会被阻止。 A workaround in this case is to open the popup right away and fill in the content later. 在这种情况下,一种解决方法是立即打开弹出窗口,然后再填写内容。 This is outside the scope of this question but I feel like this is information that everyone should know. 这超出了这个问题的范围,但是我觉得这是每个人都应该知道的信息。

Something like this? 像这样吗

<!DOCTYPE html>
<html>
<head>
<script>
function open_win() 
{
window.open("URL");
open_win_two();
}
function open_win_two()
{
window.open("URL");
}
</script>
</head>
<body>
<a onclick="open_win()">luyfl</a>
</body>
</html>

Yu can try the new target _newtab : Yu可以尝试新的目标_newtab

<a href="site.html" target="_newtab">blabla</a>

It works in Firefox, don't know if it's supported in other browsers. 它可以在Firefox中运行,不知道其他浏览器是否支持它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM