如何在新标签页中打开链接?使用 JavaScript 或 jQuery。没有阻止弹出窗口的浏览器警报

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30479837/
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 05:15:09  来源:igfitidea点击:

How can I open a link in a new tab? Using JavaScript or jQuery. Without the browser alert of pop-up blocked

javascriptjqueryhtml

提问by Shrinivas Mese

I want to open a new tab using JavaScript or jQuery.

我想使用 JavaScript 或 jQuery 打开一个新选项卡。

I tried this code:

我试过这个代码:

window.open("myurl", '_blank');

But browser gives me alert for pop-up blocked.

但是浏览器会提醒我弹出窗口被阻止。

I have to open new tab without pop-up blocked alert.

我必须在没有弹出阻止警报的情况下打开新标签。

Each and every client can't allow pop-up.

每个客户端都不允许弹出。

Can anyone help me please?

有人可以帮我吗?

回答by AkshayJ

The only way to overcome this is to perform a synchronous Ajax request which will block your browser while it runs, but will preserve the event context. This will help---> Open new tab without popup blocker after ajax call on user click

解决此问题的唯一方法是执行同步 Ajax 请求,该请求将在浏览器运行时阻塞浏览器,但会保留事件上下文。这将有所帮助 ---> 在用户单击 ajax 调用后打开没有弹出窗口阻止程序的新选项卡

Here is the sample code for you --->

这是您的示例代码--->

<table>

    <tr>
        <td>Works without warning in all browsers:</td>
        <td><input type="button" onclick="performSyncronousRequest()" value="Syncronous request"/><td>
    </tr>

    </tr>
</table>

Scipt--->

脚本--->

/**
* This method will give open the popup without a warning.
*/
function performSyncronousRequest() {
    $.ajax({
     url: '/echo/html',
     data: {},
     success: function(){
         window.open('http://www.w3schools.com');
     },
     async: false
    });
}

Heres the working fiddle http://jsfiddle.net/23JNw/80/

这是工作小提琴http://jsfiddle.net/23JNw/80/

回答by Mohammed Safeer

try this,

尝试这个,

$('#myButton').click(function () {
    var redirectWindow = window.open('http://google.com', '_blank');
    redirectWindow.location;
});

working js fiddle for this http://jsfiddle.net/safeeronline/70kdacL4/2/

为这个http://jsfiddle.net/safeeronline/70kdacL4/2/工作 js 小提琴

working js fiddle for ajax window open http://jsfiddle.net/safeeronline/70kdacL4/1/

为 ajax 窗口工作的 js 小提琴打开 http://jsfiddle.net/safeeronline/70kdacL4/1/

回答by Rasel

var win = window.open('http://stackoverflow.com/', '_blank');
if(win){
    //Browser has allowed it to be opened
    win.focus();
}else{
    //Broswer has blocked it
    alert('Please allow popups for this site');
}

回答by Bablu Ahmed

You can try like this

你可以这样试试

$(document).on('click', '.preview', function(event) {
    event.preventDefault();
    if (confirm("Are You Sure?"))
    {
        var daoGroup = $("#daoGroup").val();
        if (daoGroup === undefined && daoGroup === null) {
            alert("Select DAO Groups");
            return false;
        }
        else
        {
            /* Act on the event */
            var data = $(".frmContent").serialize();
            var url = '<?php echo base_url() ?>reportViewPrint/sailorNominalRoll/htmlPreview';
            window.open(url+'?'+ data, '_blank');
        }
    }
});