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,784
  • 5
  • 31
  • 44
  • 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
  • 100,477
  • 16
  • 156
  • 200
4

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

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
1

You need to use location.hash property

antyrat
  • 27,479
  • 9
  • 75
  • 76
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,638
  • 3
  • 36
  • 57