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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM