使用Javascript尝试根据URL中的内容重定向页面
我期待重定向一个网页,如果该网址是一个特定的网址。我只能编辑标题脚本(在每个网页中),但我只需要重定向一个页面,所以我选择了这条路线 例如,如果URL是/blog,我希望它重定向到/blog home。我遇到的问题是/blog/blogpost也重定向到/blog home。我只想重定向一个页面(/blog) 现在我有:使用Javascript尝试根据URL中的内容重定向页面,javascript,Javascript,我期待重定向一个网页,如果该网址是一个特定的网址。我只能编辑标题脚本(在每个网页中),但我只需要重定向一个页面,所以我选择了这条路线 例如,如果URL是/blog,我希望它重定向到/blog home。我遇到的问题是/blog/blogpost也重定向到/blog home。我只想重定向一个页面(/blog) 现在我有: if(document.URL.indexOf("/blog") >= 0){ window.stop(); window.location.href = "
if(document.URL.indexOf("/blog") >= 0){
window.stop();
window.location.href = "/blog-home";
}
使用此代码,/blog将fine重定向到/blog home。在
/
字符上拆分URL,并在最后一部分使用相等测试
var split = document.URL.split("/");
if (split[split.length-1] == "blog") {
window.stop();
window.location.href = "/blog-home";
}
拆分
/
字符上的URL,并在最后一部分使用相等测试
var split = document.URL.split("/");
if (split[split.length-1] == "blog") {
window.stop();
window.location.href = "/blog-home";
}
您可以使用以确保“/blog”是URL的最后一部分
if(document.URL.endsWith("/blog")){
window.stop();
window.location.href = "/blog-home";
}
您可以使用以确保“/blog”是URL的最后一部分
if(document.URL.endsWith("/blog")){
window.stop();
window.location.href = "/blog-home";
}
<>您可以使用正则表达式检查它,也可以考虑它是URL之后的附加尾随<代码> /<代码>,它可能以例如<代码> /博客< /代码>或<代码> /博客/ < /代码>:结束。
if(document.URL.match(/\/blog\/?$/)!==null){
window.stop();
window.location.href=“/blog home”;
} /代码> 您可以使用正则表达式检查它,也可以考虑它是URL之后的附加尾随<代码> /<代码>,它可能以例如<代码> /博客< /代码>或<代码> /博客/ < /代码>:结束。
if(document.URL.match(/\/blog\/?$/)!==null){
window.stop();
window.location.href=“/blog home”;
}
window.location提供当前url路径
window.location.origin为您提供基本路径,因此您可以执行以下操作:
if(window.location === window.location.origin +"/blog" ) {
window.stop();
window.location.href = "/blog-home"
}
window.location提供当前url路径
window.location.origin为您提供基本路径,因此您可以执行以下操作:
if(window.location === window.location.origin +"/blog" ) {
window.stop();
window.location.href = "/blog-home"
}
非常感谢。这很有效。我最后给出了另一个答案,因为我认为这对我的客户更有意义。谢谢!这很有效。我最后给出了另一个答案,因为我认为这对我的客户更有意义。