js實現window.open不被攔截的解決方案匯總_javascript技巧

來源:互聯網
上載者:User

本文執行個體講述了js實現window.open不被攔截的解決方案。分享給大家供大家參考。具體分析如下:

一、問題:

今天在處理頁面ajax請求過程中,想實現請求後開啟新頁面,就想到通過 js window.open 來實現,但是最終都被瀏覽器攔截了。

二、分析:

在Google搜尋有沒有解決方案,有些說可以通過建立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>

希望本文所述對大家基於javascript的web程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.