天天看點

vue js打開外部連結網頁 去除字首 本地localhost方法

window.open(‘baidu.com’) 總是會發現跳轉的是 localhost:xxxx/baidu.com,顯然不是我們要的效果,

在url前面+‘//’ 就可以了

使用window.open(’//’+‘baidu.com’)

2022.01.14更新

用上述方法後發現出現了另一種情況,如果我的位址是https://www.baidu.com/,用上述方法打開,會變成https//www.baidu.com。具體原因未知,但是找到了兩種解決方案,記錄一下,都是基于a标簽

1.在網頁内直接嵌入a标簽

<el-table-column label="詳情" align="center">
        <template slot-scope="scope">
          <el-button type="text" >
            <a :href="scope.row.url" target="_blank">檢視連結</a>
          </el-button>
        </template>
</el-table-column>      

2.js生成a标簽

const element = document.createElement('a');
element.href = url
element.target = '_blank'
element.click()      

繼續閱讀