Safari浏览器出于安全问题不能异步自动触发比如请求后使用,window.open(直接使用window.open属于浏览器行为,所以被阻止打开新窗口);

那我们需要做兼容,其他的浏览器使用open。苹果浏览器使用a链接模拟点击的方式来实现 新窗口。

<span class="m_score" >实现新窗口跳转</span>
<a target="_blank" id="open_href" href=""></a>
<script>
//兼容苹果浏览器不能触发模拟点击事件
function dispatch(dom){
    if(dom.click){
        dom.click();
    }else{
        try{
            var evt = document.createEvent('Event');  
            evt.initEvent("click",true,true);
            dom.dispatchEvent(evt); 
        }catch(e){
            
        };
    }
}
  
;(function ($) {
    'use strict'
    var  fun =  {   
      
        getUrl : function(){
            $(".m_score").on("click",function(){
               
               if((/Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent))){
                  // 兼容火狐浏览器
                  $("#open_href").attr("href","www.baidu.com");
                  var dom = document.getElementById('open_href');
                  dispatch(dom); 
                }else{
                      window.open("www.baidu.com");
                }
            })
        },

        init : function() {
            this.getUrl();
        },
       
    }
    $(function(){
        fun.init();
    })
})(jQuery);
</script>

 

本文地址:https://blog.csdn.net/mentalitys/article/details/110958957