javascript 如何检查 URL 末尾是否有特定字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12818413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:11:29  来源:igfitidea点击:

How to check if URL has a specific string at the end

javascriptjqueryurl

提问by Alessandro

I need to get an overlay to slide down based on what the URL has at the end of it.

我需要根据 URL 末尾的内容让覆盖层向下滑动。

If (URL has 'faq' at the end) { overlay comes down }

如果(URL 末尾有 'faq'){ 覆盖下降}

How can you do that in jQuery/JavaScript?

你怎么能在 jQuery/JavaScript 中做到这一点?

回答by Christofer Eliasson

If your URL looks something like this http://yourdomain.com/faq, you could do something like this:

如果您的 URL 看起来像这样http://yourdomain.com/faq,您可以执行以下操作:

var url = window.location.href;
var lastPart = url.substr(url.lastIndexOf('/') + 1);

if (lastPart === "faq") {
   // Show your overlay
}

This would make it possible to check for other endings and act on them as well.

这将使检查其他结局并对其采取行动成为可能。

Update:

更新:

To get it working, even if the URL has a trailing slash, you could create a function like this:

为了让它工作,即使 URL 有一个斜杠,你也可以创建一个这样的函数:

function getLastPart(url) {
    var parts = url.split("/");
    return (url.lastIndexOf('/') !== url.length - 1 
       ? parts[parts.length - 1]
       : parts[parts.length - 2]);
}

You could then call the function like getLastPart(window.location.href)to get the last part of the URL for the current page.

然后您可以调用该函数getLastPart(window.location.href)来获取当前页面的 URL 的最后一部分。

Here is a working example as well: http://jsfiddle.net/WuXHG/

这里也是一个工作示例:http: //jsfiddle.net/WuXHG/

Disclaimer: If your URLs use hashes at the end, or a querystring, you would have to strip the from the URL first, for this script to work properly

免责声明:如果您的网址在末尾使用哈希值或查询字符串,则您必须先从网址中删除 ,以便此脚本正常工作

回答by complex857

You should be able to use the window.location object for this with a regexp, something like this:

您应该能够通过正则表达式使用 window.location 对象,如下所示:

/faq$/.test(window.location)

回答by Abdul Rauf

A new method endsWith()has been added to the ES6 specification. For previous versions we can polyfill it using

endsWith()ES6 规范中添加了一个新方法。对于以前的版本,我们可以使用 polyfill 它

if (!String.prototype.endsWith)
  String.prototype.endsWith = function(searchStr, Position) {
      // This works much better than >= because
      // it compensates for NaN:
      if (!(Position < this.length))
        Position = this.length;
      else
        Position |= 0; // round position
      return this.substr(Position - searchStr.length,
                         searchStr.length) === searchStr;
  };

Now you can easily write

现在您可以轻松编写

If (window.location.href.endsWith("faq")) { 
   // Show your overlay
}

Reference:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith

参考:https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith

回答by Pierre

You can get the current URL using :

您可以使用以下方法获取当前 URL:

 var currentUrl = window.location.href;

Then you can use indexOf to check if your token is in the end of string (here faq)

然后你可以使用 indexOf 来检查你的令牌是否在字符串的末尾(这里是 faq)

 if (currentUrl.indexOf('faq') == currentUrl.length - 3)
 {
  // Do something here
 }