【发布时间】:2022-02-11 02:07:29
【问题描述】:
如果 url 等于以下 url 数组中的任何字符串,我会尝试进行重定向:
所以我做了以下
function redirectUser() {
if (window.location.href === 'https://swish.com/login' || window.location.href === 'https://swish.com/register' || window.location.href === 'https://swish.com/overview') {
window.location.replace('https://swish.com/onboard');
}
}
但这有点难看,所以我想把网址放在一个数组中,然后做这样的事情:
function redirectUser() {
const urls = ['https://swish.com/login', 'https://swish.com/register', 'https://swish.com/overview' ]
for(let url of urls) {
if (window.location.href === url) {
window.location.replace('https://swish.com/onboard');
}
}
}
还有其他方法可以做到这一点吗?如果不是,您认为哪个是更好的选择?谢谢!
【问题讨论】:
-
if(urls.includes(window.location.href)) redirect
标签: javascript reactjs dom ecmascript-6 href