Javascript window.open() 函数打开没有弹出窗口阻止程序的链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12247368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:30:59  来源:igfitidea点击:

Javascript window.open() function opens link without popup blocker

javascript

提问by Ashish

Javascript window.open()function opens link without popup blocker, I want to open some links from javascript window.open()function but it get blocked on firefox, chrome, safari.

Javascriptwindow.open()函数在没有弹出窗口阻止程序的情况下打开链接,我想从 javascriptwindow.open()函数打开一些链接,但它在 firefox、chrome、safari 上被阻止。

How Can I unblock popup through javascript code so that it will opens up in new tab

如何通过javascript代码取消阻止弹出窗口,以便它在新选项卡中打开

My code is:

我的代码是:

    function GoUrl(id) {
        var string = 'select#' + id + ' option:selected';
        var value = $(string).val(); 
        if (value != "links") {enter code here
           window.open(value, '_blank'); 
            window.focus();
        }

回答by Hank

Just found the answer from a different thread, thought I shared this one with you guys, the ONLYway to bypass the popup blocker is through the onclickevent, the problem I was having was that I had onmousedowninstead, I just simply changed it to onclick, and boom! worked instantly!

刚刚从不同的线程中找到答案,以为我与你们分享了这个,绕过弹出窗口阻止程序的唯一方法是通过onclick事件,我遇到的问题是我使用的是onmousedown,我只是简单地将其更改为点击,然后繁荣!立即工作!

回答by Martin Borthiry

You should launch you function from user event. i.e: onclick or href:

您应该从用户事件启动您的功能。即:onclick 或 href:

 <a href="javascript:window.open('http://google.com');return false;"> open 2 </a>

回答by Some Guy

You can't force it to open a new tab. The functionality is controlled by the browser entirely.

你不能强迫它打开一个新标签。该功能完全由浏览器控制。

To avoid having your windows blocked, have the window open on any user event (like onclick).

为避免您的窗口被阻止,请在任何用户事件(如onclick)上打开窗口。