如何避免javascript`window.open`触发弹出窗口警报

时间:2015-12-21 21:33:53

标签: javascript php jquery ajax

我正在尝试在jQuery中构建一个异地通知功能。该脚本首先检查链接是否是外部链接,然后检查db表条目是否有异常。如果链接是外部链接而不在例外列表中,请将访问者发送到通知页面。如果它是例外列表中的外部链接,则在没有通知页面的新窗口中打开链接。

我正在使用jQuery $.post调用将链接信息发送到php脚本,该脚本检索异常并返回yes或no,如果需要转到通知屏幕。这是代码:

$('a').click(function(){
    var url =$(this).attr('href');

    if(url !== '#'){ 
        // ignore links that don't 'go' anywhere 

        if($(this).hasClass('alerted')){
            // .alerted is used on the notification page to show the user's already been notified & prevents an infinite loop of notifications.
            window.open(url);
            return false;

        }else if(url.substr(0,4) !='http'){
            // check that the url isn't an internal link ('/page.php' for example)
            return true;
        }

        // ajax script to check url is external and is there an exception. Returns as json object:
        // link: link
        // notify: true/false
        $.post("/scripts/form_process.php", { action : 'offsite', link: url}, function(data){
            if(data.notify == true){
                // if visitors should be notified, redirect to the following link:
                window.location= '/leaving-site?link='+encodeURIComponent(data.link);
                return false;
            }else{
                // if the link is in the exception list, don't notify but do open the link in a new window:
                window.open(data.link);
            }

        });
        return false;
    }
});

这样工作正常,只要window.open(url)命令在$.post成功函数内,浏览器就像弹出窗口而不是自然链接一样。据我所知,在window.open调用中使用ajax时,这似乎是一个问题。当我在这里使用它时:

        if($(this).hasClass('alerted')){
            // .alerted is used on the notification page to show the user's already been notified & prevents an infinite loop of notifications.
            window.open(url);
            return false;
        }

我没有得到弹出窗口阻止程序。

我无法对例外列表进行硬编码,我必须检查每个链接 - 我不能假设将一个类添加到需要通知的链接中。

如何在新标签页中打开外部链接并避免使用此代码中的弹出窗口阻止程序?

1 个答案:

答案 0 :(得分:3)

解决此问题的经典方法如下: 在AJAX调用之前创建新窗口:

var newWindow = window.open('', '_blank');

成功之后 - 您将URL分配给新窗口,如下所示:

newWindow.location.href = 'http://example.com';

代码的完整示例:

$('a').click(function(){

    var url =$(this).attr('href');

    if(url !== '#'){ 
        // ignore links that don't 'go' anywhere 

        if($(this).hasClass('alerted')){
            // .alerted is used on the notification page to show the user's already been notified & prevents an infinite loop of notifications.
            window.location = url;
            return false;

        }else if(url.substr(0,4) !='http'){
            // check that the url isn't an internal link ('/page.php' for example)
            return true;
        }

        // ajax script to check url is external and is there an exception. Returns as json object:
        // link: link
        // notify: true/false
         var newWindow = window.open('', '_blank');
        $.post("/scripts/form_process.php", { action : 'offsite', link: url}, function(data){
            if(data.notify == true){
                // if visitors should be notified, redirect to the following link:
                newWindow.location.href= '/leaving-site?link='+encodeURIComponent(data.link);
                return false;
            }else{
                // if the link is in the exception list, don't notify but do open the link in a new window:
                newWindow.location.href(data.link);
            }

        });
        return false;
    }
});