Find the “#” character in a string with Javascript

前端 未结 4 1715
野趣味
野趣味 2021-01-23 07:06

I need to parse the given urls via Javascript but I\'m not able to use .split() to accomplish to this.

IE:

  var str = window.location.pathname;
  var su         


        
相关标签:
4条回答
  • 2021-01-23 07:33

    Use window.location.hash, it gives you that portion broken out.

    Also, you're trying to "split" on the # character which won't be found in window.location.pathname, only window.location.hash--unless you use window.location and search the entire url.

    0 讨论(0)
  • 2021-01-23 07:36

    You are looking for location.hash. It contains the # itself and everything that comes after it.

    0 讨论(0)
  • 2021-01-23 07:40

    You need to use location.hash property

    0 讨论(0)
  • 2021-01-23 07:45

    if jquery is an option, try the https://github.com/allmarkedup/jQuery-URL-Parser example

    var url = $.url('"http://mydomain.com/myaddress/page/2/#lookforme');
    alert(url.attr('fragment'));
    
    0 讨论(0)
提交回复
热议问题