admin管理员组

文章数量:1415645

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

IE:

  var str = window.location.pathname;
  var substr = str.split('/');
  alert(substr)

If i enter this url "/" I have thoose values "myaddress,page,2"

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

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

IE:

  var str = window.location.pathname;
  var substr = str.split('/');
  alert(substr)

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

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

Share Improve this question asked Apr 24, 2012 at 12:57 Pennywise83Pennywise83 1,7845 gold badges33 silver badges44 bronze badges 2
  • 2 Are you sure location.pathname contains a #? Usually that is given as location.hash. – Thilo Commented Apr 24, 2012 at 12:59
  • possible duplicate of Grabbing hash from URL? – Felix Kling Commented Apr 24, 2012 at 13:04
Add a ment  | 

4 Answers 4

Reset to default 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.

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

You need to use location.hash property

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

var url = $.url('"http://mydomain./myaddress/page/2/#lookforme');
alert(url.attr('fragment'));

本文标签: Find the quotquot character in a string with JavascriptStack Overflow