【问题标题】:Javascript window.open issue in safariSafari 中的 Javascript window.open 问题
【发布时间】:2018-01-16 02:47:42
【问题描述】:

我正在尝试使用 jquery 打开新标签。但是,它不适用于 safari 或 Mac。

我的代码:

<script>
    $(document).ready(function(){
        $.ajax({
            type: 'post',
            dataType: 'html',
            url:'http://localhost/test/remoteContent.html',
            async: false,
            success:function(data){
                window.open("http:google.com",'_blank');
            }

        })
    });
</script>

【问题讨论】:

标签: jquery safari


【解决方案1】:

我知道这不是您想要的答案,但不幸的是,这就是现代浏览器安全性的工作方式。由于弹出窗口和其他操作可能被滥用以在网页上做“坏事”,像 click 用于文件上传表单字段和window.open 在其他情况下在用户已明确执行了一项操作。这些被称为trusted events,不能被 JavaScript 欺骗。我之前已经阅读过这方面的内容,您应该会发现 this answer 提供的信息非常丰富。

您必须修改您的工作流程,以便在用户单击某些内容后打开选项卡,或者在初始操作中打开一个新选项卡。

【讨论】:

    【解决方案2】:

    我为此找到了替代解决方案。 ajax响应后创建锚标签元素并触发点击事件

     var a = document.createElement('a');
     a.href = 'https://google.com';
     a.setAttribute('target', '_blank');
     a.click();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      相关资源
      最近更新 更多