它应该按如下方式工作:如果 url 与域匹配,则重定向到新域 + 添加 url 路径 + 查询字符串:
if (window.location.href == "www.heelsteal.com.au") {
var pathName = window.location.pathname
window.location = ("www.wittner.com.au/" + pathName + "?string=redirect");
}
我得到的只是undefined
。
最佳答案
您可以尝试使用 indexOf()
。
indexOf()
将返回 -1
,如果找到该字符串,则返回 0
。
window.location.origin
将返回域名,因此我们可以在 if
条件中使用它。
window.location.pathname
如您所知将返回路径,但如果不存在路径,它将返回 /
所以我已将其从重定向 URL 中删除,否则您将获得 www.wittner.com.au//somepath?string=redirect 而不是 www.wittner.com.au/somepath?string=redirect
if(window.location.origin.indexOf('heelsteal.com.au')>-1){
window.location.href='http://www.wittner.com.au'+window.location.pathname+'?string=redirect';
}
如果您对上述源代码有任何疑问,请在下方留言,我会尽快回复您。
希望这对您有所帮助,编码愉快!
关于javascript - 重定向未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33519680/