1

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 substr = str.split('/');
  alert(substr)

If i enter this url "http://mydomain.com/myaddress/page/2/" I have thoose values "myaddress,page,2"

But it doesn't work if instead of / I insert #

Pennywise83
  • 1,754
  • 5
  • 29
  • 43
  • 2
    Are you sure `location.pathname` contains a `#`? Usually that is given as `location.hash`. – Thilo Apr 24 '12 at 12:59
  • possible duplicate of [Grabbing hash from URL?](http://stackoverflow.com/questions/9043067/grabbing-hash-from-url) – Felix Kling Apr 24 '12 at 13:04

4 Answers4

4

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.

Brad Christie
  • 98,427
  • 16
  • 148
  • 198
4

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

ThiefMaster
  • 298,938
  • 77
  • 579
  • 623
1

You need to use location.hash property

antyrat
  • 26,950
  • 9
  • 73
  • 75
0

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'));
esskar
  • 10,159
  • 3
  • 34
  • 55