zoukankan      html  css  js  c++  java
  • js实现window.open不被拦截的解决方法汇总

    一、问题:

    今天在处理页面ajax请求过程中,想实现请求后打开新页面,就想到通过 js window.open 来实现,但是最终都被浏览器拦截了。

    二、分析:

    在谷歌搜索有没有解决方法,有些说可以通过新建a标签,模拟点击来实现,但是测试发现都实现不了,照样被浏览器拦截。
    最后找到了一个折中的办法,可以实现新页面打开,但是没有a标签的那种直接流量新页面的效果。

    三、实现代码:

    复制代码代码如下:
    $obj.click(function(){
     var newTab=window.open('about:blank');
     $.ajax({
      success:function(data){
       if(data){
        //window.open('http://www.jb51.net');
        newTab.location.href="http://www.jb51.net";
       }
      }
     })
    })


    其它方法:

    复制代码代码如下:
    <script type="text/javascript">
    <!-- 
    $( 
    function()
    {
    //方法一
    window.showModalDialog("http://www.jb51.net/");
    window.showModalDialog("http://www.jb51.net/");
     

    //方法二
    var aa=window.open();
    setTimeout(function(){
    aa.location="http://www.jb51.net";
    }, 100);
     

    var b=window.open();
    setTimeout(function(){
    b.location="http://www.jb51.net";
    }, 200);
     

    var c=window.open();
    setTimeout(function(){
    c.location="http://www.jb51.net";
    }, 300);
     

    var d=window.open();
    setTimeout(function(){
    d.location="http://www.jb51.net";
    }, 400);
     

    var ee=window.open();
    setTimeout(function(){
    ee.location="http://www.jb51.net";
    }, 500);
     

    var f=window.open();
    setTimeout(function(){
    f.location="http://www.jb51.net";
    }, 600);
     

    var g=window.open();
    setTimeout(function(){
    g.location="http://www.jb51.net";
    }, 700);
     

    var h=window.open();
    setTimeout(function(){
    h.location="http://www.jb51.net";
    }, 800);
     

    var i=window.open();
    setTimeout(function(){
    i.location="http://www.jb51.net";
    }, 900);
     

    var j=window.open();
    setTimeout(function(){
    j.location="http://www.jb51.net";
    }, 1000);
     

    //方法三
    var a = $("<a href='http://www.jb51.net' target='_blank'>Apple</a>").get(0);
    var e = document.createEvent('MouseEvents');
    e.initEvent( 'click', true, true );
    a.dispatchEvent(e);
     

    var a = $("<a href='http://www.jb51.net' target='_blank'>Apple</a>").get(0);
    var e = document.createEvent('MouseEvents');
    e.initEvent( 'click', true, true );
    a.dispatchEvent(e);
    }
     
    );
    //-->
    </script>

  • 相关阅读:
    6_10 下落的树叶(UVa699)<二叉树的DFS>
    6_9 天平(UVa839)<二叉树的DFS>
    6_8 树(UVa548)<从中序和后序恢复二叉树>
    6_7 树的层次遍历(UVa122)<二叉树的动态创建与BFS>
    6_6 小球下落(UVa679)<完全二叉树编号>
    6_4 破损的键盘(UVa11988)<链表>
    6_3 矩阵链乘(UVa424)<用栈实现简单的表达式解析>
    6_2 铁轨(UVa514)<栈>
    第五周课程总结&试验报告(三)
    第四周课程总结和实验报告
  • 原文地址:https://www.cnblogs.com/zqyanywn/p/6842091.html
Copyright © 2011-2022 走看看