js 模擬window.open 打開新窗口


為什么要去模擬window.open() 打開一個 新的窗口呢,因為有些瀏覽器默認會攔截 window.open, 當需要函數中打開新窗口時,接可以使用a標簽去模擬打開。

/**
* a模擬window.open,不會被瀏覽器攔截
* @param {String} url        a標簽打開的地址
* @param {String} id         a標簽的ID
* @param {String} targetType a標簽點擊打開的方式(當前頁面打開還是新窗口打開)
*/
openWindow: (url, targetType = '_blank', id = 'open', download = false) => {
    // 如果存在則刪除
    if (document.getElementById(id)) {
        document.body.removeChild(document.getElementById(id))
    }
    const a = document.createElement('a')
    a.setAttribute('href', url)
    if (download) {
        a.setAttribute('download', url)
    }
    a.setAttribute('target', targetType)
    a.setAttribute('id', id)
    document.body.appendChild(a)
    a.click()
}

 


免責聲明!

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



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