jQuery to parse our a part of a url path

前端 未结 2 720
醉梦人生
醉梦人生 2021-01-28 22:41

I need to parse long urls and set a variable (category) equal to one of the /folders/ in the path.

For example, a url that is

http://example.

2条回答
  •  -上瘾入骨i
    2021-01-28 23:44

    When you do this.href*= you're doing multiplication, and that's why you're getting not-a-number. It multiplies this.href by the string and assigns that to href.

    If you mean to test whether the url starts with that string you can do it like this, no need for jQuery:

    var start = 'http://example.com/community/';
    if (url.substring(0, start.length) === start)){
      var category = url.split("community/");
      var lastPart = category[category.length - 1];
      return lastPart.split("/")[0];
    }
    

提交回复
热议问题