欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

window.open打開新頁面失效解決方案

 更新時(shí)間:2023年07月19日 09:19:05   作者:MissXu666  
這篇文章主要給大家介紹了關(guān)于window.open打開新頁面失效的解決方案,移動端和PC端全部通過window.open()來跳轉(zhuǎn)頁面窗口,文中給出了詳細(xì)的解決方案,需要的朋友可以參考下

在開發(fā)h5項(xiàng)目的時(shí)候 經(jīng)常需要使用window.open 來打開新頁面,但有時(shí)會出現(xiàn)失效的情況。

問題復(fù)現(xiàn):

在接口請求完成后,根據(jù)返回的結(jié)果調(diào)用window.open 失效

原因:瀏覽器出于安全的考慮,會攔截掉非用戶操作打開的新頁面;實(shí)際上,在異步的方法中以及非用戶操作打開的新頁面都會被攔截(不同瀏覽器不同版本表現(xiàn)不同,不是所有情況都會被攔截,但是任然需要做兼容處理)

例如:

fetch(url,option).then(res=>{    
    window.open('http://www.test.com')
})
setTimeout(() => {
    window.open(this.url, '_blank')
}, 100)
。。。
if (success) window.open(data);

解決方案:

1、盡量讓將調(diào)用window.open的方法 寫在用戶事件中,例如:

  if (success) {
          Dialog.alert({
            content: '即將跳轉(zhuǎn)單證鏈接',
            onConfirm: () => {
              window.open(data);
            },
          });
        }

交互上的小修改,這樣寫需要用戶手動確定才會跳轉(zhuǎn)

2、 使用a標(biāo)簽進(jìn)行跳轉(zhuǎn)

ajax().then(res => {
	asyncOpen(res.url)
})
function asyncOpen(url) {
    var a = document.createElement('a')
    a.setAttribute('href', url)
    a.setAttribute("target", "_blank");
    a.setAttribute("download", 'name');
    document.body.appendChild(a);
    a.click();
    a.remove();
}

3、使用中轉(zhuǎn)頁面

一定要把window.open定義在接口請求的外部,保證新開空白窗口不會被攔截。
var newWin = window.open('loading page') 
ajax().then(res => {
	newWin.location.href = 'target url'
}).catch(() => {
	newWin.close()
})

補(bǔ)充小知識:window.open下載不打開新窗口

open的第二個參數(shù)是 '_self' 即可

window.open(url,'_self');

總結(jié)

到此這篇關(guān)于window.open打開新頁面失效解決方案的文章就介紹到這了,更多相關(guān)window.open打開新頁面失效內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論