当用户不在此页时,我希望将他们重定向到特定的页面。
所以,类似于(伪代码):
if not (url = example.com/index.php)
redirect to example.com/index.php
/if当用户访问页面example.com/forum.php时,他将被重定向到example.com/index.php。
( jQuery文件加载到每个文件中)
我怎么能把它存档?
发布于 2014-09-27 14:52:59
您可以使用window.location.href。这里的例子..。
if(url === 'example.com/forum.php'){
window.location.href = 'example.com/index.php';
return False;
}不要忘记在块的末尾使用return false,这样如果在window.location.href之后有任何可运行的代码,那么浏览器就可以忽略这一点。
发布于 2017-06-21 19:00:36
为了进一步说明,因为我不能comment...url返回未定义的不首先定义它(您会注意到上面的评论)。
要定义url,可以这样做:
var url = $(location).attr('href');然后,您可以继续执行检查:
if(url === 'yourspecifiedurl.com'){
window.location.href = 'yournewurl.com';
return false;
}此外,您不需要使用window.location.href,因为window.location就足够了。默认为window.location.href
最后,还可以使用“替换”:
window.location.replace('theurltodirectto.com');https://stackoverflow.com/questions/26075856
复制相似问题