Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何避免javascript`window.open`触发弹出窗口警报_Javascript_Php_Jquery_Ajax - Fatal编程技术网

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

如何避免javascript`window.open`触发弹出窗口警报,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我正试图在jQuery中构建一个非现场通知函数。脚本首先检查该链接是否为外部链接,然后根据db表条目检查异常。如果链接是外部的,不在例外列表中,则将访问者发送到通知页面。如果是异常列表中的外部链接,则在新窗口中打开该链接,而不显示通知页面 我正在使用jQuery$.post调用将链接信息发送到一个php脚本,该脚本检索异常,如果需要转到通知屏幕,则返回yes或no。代码如下: $('a').click(function(){ var url =$(this).attr('href');

我正试图在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;
    }
});
这可以正常工作,只是只要
窗口.open(url)
命令位于
$中。post
成功功能将其视为弹出窗口,而不是自然链接。在使用
窗口时,这似乎是一个问题。在
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;
        }
我不懂弹出窗口拦截器

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


如何在新选项卡中打开外部链接并避免代码中的弹出阻止程序?

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

var newWindow = window.open('', '_blank');
在success中,您将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;
    }
});

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

var newWindow = window.open('', '_blank');
在success中,您将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;
    }
});

你是对的,只要它落在点击的“线程”之外,它就会被视为一个未经请求的弹出窗口。我们这里有很多链接吗?在页面加载时,您是否可以在用户查看当前内容时在后台抓取外部链接并验证它们?弹出窗口拦截器允许根据用户的直接/实际点击是否触发了代码调用窗口。打开。ajax响应处理程序显然不是其中之一。它不是由点击触发的,而是由一些网络活动触发的。这个网络活动是由点击引起的并不重要——点击链接和触发成功处理程序之间没有直接联系。因此它会被阻止。@Brad Cristie-此网站是客户端可编辑的,因此必须检查网站上的每个链接,因此可能会有很多链接。下面的答案似乎解决了这个问题,尽管你是对的,只要它落在点击的“线程”之外,它就会被视为一个未经请求的弹出窗口。我们这里有很多链接吗?在页面加载时,您是否可以在用户查看当前内容时在后台抓取外部链接并验证它们?弹出窗口拦截器允许根据用户的直接/实际点击是否触发了代码调用窗口。打开。ajax响应处理程序显然不是其中之一。它不是由点击触发的,而是由一些网络活动触发的。这个网络活动是由点击引起的并不重要——点击链接和触发成功处理程序之间没有直接联系。因此它会被阻止。@Brad Cristie-此网站是客户端可编辑的,因此必须检查网站上的每个链接,因此可能会有很多链接。下面的答案似乎解决了这个问题,只需稍作调整:将newWindow行移动到
$.post
的正上方,这样就不会为内部链接或已收到通知的链接打开新的空白窗口。我已经编辑过了,所以这不是问题啊,对了,谢谢你的编辑!很高兴我能提供帮助,@TH1981这只需稍微调整一下即可:将新窗口行移动到
$.post
上方,这样它就不会为内部链接或已收到通知的链接打开新的空白窗口。我已经编辑过了,所以这不是问题啊,对了,谢谢你的编辑!很高兴我能帮忙,@TH1981