【问题标题】:Redirect if URL contains word如果 URL 包含单词,则重定向
【发布时间】:2015-09-02 07:20:44
【问题描述】:

我有这个脚本,它运行良好,但如果 URL 是这样的,它会重定向到 Google:

http://example.com/test/examples

if (window.location.href.indexOf("/test") > -1) {
    window.location.href="http://google.hu";
}

如果我在 /test/ 之后有单词,我不想重定向。

所以我需要这个:

http://example.com/test/examples --> 不重定向

http://example.com/test ---> 重定向

我如何用 jQuery 做到这一点?

【问题讨论】:

  • 最好在服务器上做重定向;客户端并不总是支持 JavaScript 或基于客户端的重定向。

标签: jquery redirect


【解决方案1】:

您可以使用正则表达式检查网址是否以 test/ 结尾

var url = window.location.href;
if(url.search(/test\/$/) >= 0) {
    alert("Do redirect!");   
}

或 url.search(/test$/) 如果你想看看它是否以 test 结尾

【讨论】:

    猜你喜欢
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多