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

标签 javascript php jquery ajax

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

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

$('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 success 函数中,浏览器就会将其视为弹出窗口而不是作为自然链接。据我所知,在 ajax 调用中使用 window.open 时,这似乎是一个问题。当我在这里使用它时:

        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;
        }

我没有得到弹出窗口拦截器。

我不能对异常(exception)列表进行硬编码,而且我必须检查每个链接 - 例如,我不能假设一个类将添加到需要通知的链接中。

如何在新标签页中打开外部链接并避免此代码中的弹出窗口拦截器?

最佳答案

经典的解决方法如下: 在 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;
    }
});

关于javascript - 如何避免 javascript `window.open` 触发弹出窗口警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34404696/

相关文章:

javascript - jQuery 鼠标事件处理

javascript - 如何验证从数据库生成的调查问卷

php - 将三个表连接在一起 SQL PHP

PHP: session 变量丢失, session ID 更改(使用 Ajax)

php - mySQL仅存储部分字符串

javascript - 为什么 jquery load() 方法不需要网络服务器才能正常工作?

jquery - JQuery 和转换源

c# - Javascript onkeyup 事件(如何在我的代码中提及箭头键)

javascript - 需要更改 wordpress 的字体选项

javascript - 如何使用 React 显示以 JSON 格式接收的 HTML