js實現(xiàn)window.open不被攔截的解決方法匯總
本文實例講述了js實現(xiàn)window.open不被攔截的解決方法。分享給大家供大家參考。具體分析如下:
一、問題:
今天在處理頁面ajax請求過程中,想實現(xiàn)請求后打開新頁面,就想到通過 js window.open 來實現(xiàn),但是最終都被瀏覽器攔截了。
二、分析:
在谷歌搜索有沒有解決方法,有些說可以通過新建a標簽,模擬點擊來實現(xiàn),但是測試發(fā)現(xiàn)都實現(xiàn)不了,照樣被瀏覽器攔截。
最后找到了一個折中的辦法,可以實現(xiàn)新頁面打開,但是沒有a標簽的那種直接流量新頁面的效果。
三、實現(xiàn)代碼:
var newTab=window.open('about:blank');
$.ajax({
success:function(data){
if(data){
//window.open('http://chabaoo.cn');
newTab.location.href="http://chabaoo.cn";
}
}
})
})
其它方法:
<!--
$(
function()
{
//方法一
window.showModalDialog("http://chabaoo.cn/");
window.showModalDialog("http://chabaoo.cn/");
//方法二
var aa=window.open();
setTimeout(function(){
aa.location="http://chabaoo.cn";
}, 100);
var b=window.open();
setTimeout(function(){
b.location="http://chabaoo.cn";
}, 200);
var c=window.open();
setTimeout(function(){
c.location="http://chabaoo.cn";
}, 300);
var d=window.open();
setTimeout(function(){
d.location="http://chabaoo.cn";
}, 400);
var ee=window.open();
setTimeout(function(){
ee.location="http://chabaoo.cn";
}, 500);
var f=window.open();
setTimeout(function(){
f.location="http://chabaoo.cn";
}, 600);
var g=window.open();
setTimeout(function(){
g.location="http://chabaoo.cn";
}, 700);
var h=window.open();
setTimeout(function(){
h.location="http://chabaoo.cn";
}, 800);
var i=window.open();
setTimeout(function(){
i.location="http://chabaoo.cn";
}, 900);
var j=window.open();
setTimeout(function(){
j.location="http://chabaoo.cn";
}, 1000);
//方法三
var a = $("<a href='http://chabaoo.cn' target='_blank'>Apple</a>").get(0);
var e = document.createEvent('MouseEvents');
e.initEvent( 'click', true, true );
a.dispatchEvent(e);
var a = $("<a href='http://chabaoo.cn' target='_blank'>Apple</a>").get(0);
var e = document.createEvent('MouseEvents');
e.initEvent( 'click', true, true );
a.dispatchEvent(e);
}
);
//-->
</script>
希望本文所述對大家基于javascript的web程序設計有所幫助。
相關文章
javascript獲得服務器端控件的ID的實現(xiàn)代碼
javascript獲得服務器端控件的ID的實現(xiàn)代碼,需要的朋友可以參考下。2011-12-12遍歷json 對象的屬性并且動態(tài)添加屬性的實現(xiàn)
下面小編就為大家?guī)硪黄闅vjson 對象的屬性并且動態(tài)添加屬性的實現(xiàn)。小編覺的挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12