2

I have limited JS experience, and I need to split a string that would look something like:

http://example.com/#!/about

Into http://example.com/ and !/about

I can't unfortuently use PHP and parsing the URL won't work. Right??

This is what I have at the moment:

<script type="text/JavaScript">
var newUrl = window.location.pathname;
var hash=newUrl.split('#');
var f=hash[1];
</script>

I could do this for the 3rd line:

    var hash=newUrl.split('com'); 

And then account for the hash, but the problem with that is if someone goes to

example.com/index.php/#!/about

So I'd then have to double my code after this point.

Any ideas how to split the URL into two parts centring around the hash without having to use what I just mentioned??

Niall Paterson
  • 3,580
  • 3
  • 29
  • 37

2 Answers2

2

The .pathname doesn't include the hash. Use the .hash property instead. Use substring to strip the # character from the front.

var hash = location.hash.substring(1);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

If it's not your window.location, you can use /([^:]+://[^/]+)[^#]+#(.*)/ - you will get the required parts in the first and second capturing groups.

Alexander Pavlov
  • 31,598
  • 5
  • 67
  • 93