1

I am trying to find the string after the my hash sign so when the user reloads the page I can just look at that string and then load the correct page dynamically. My url looks like this http://mysite.com/#!/account/. How do I retrieve just the /account/? I have tried using this:

var oldLoc = window.location,
patt = /#!(.+)/,
newLoc = oldLoc.match(patt)[1]; // This returns `/account/settings`

if (newLoc == '' || newLoc == '#!/' || newLoc == '/#!' || newLoc == '/#!/') {
    $('#reload_section').load('main.php');
} else if (newLoc == '/newsfeed/') {
    $('#reload_section').load('newsfeed.php');
} else if (newLoc == '/account/') {
    $('#reload_section').load('account.php');
} else if (newLoc == '/search/') {
    $('#reload_section').load('ajaxsearch.php');
}

This works fine if I put a value like http://mysite.com/#!/account/ inside the oldLoc variable but if I put what I have it gives me this error:

TypeError: 'undefined' is not a function (evaluating 'oldLoc.match(patt)')

1)Why doesn't this work, I have tried all different combinations window.location, document.location, nothing works.
2) Is there an easier way to find what the hash is of the page and then load the correct page as shown above?

Thanks!

1
  • I've rolled back your edit; solutions should be posted as answers to the question, not as part of the question. Commented Apr 4, 2013 at 18:49

5 Answers 5

4

Use window.location.hash. For the URL http://mysite.com/#!/account/, it will return #!/account/. Now, simply take the #! off of the front with a regular expression:

window.location.hash.replace(/^#\!/,"")

The above code will give you /account/.

EDIT: You could also use this:

window.location.hash.substring(2)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much, the window.location.hash.replace(/^#\!/, "") worked perfect!
You're welcome. The pitfall with using substring(2) was that the first two characters would be removed regardless of what they were.
This retrieves hash after browser hash url, not from any string what the question at first suggests :)
3

you can use

window.location.hash

4 Comments

It would give you the latter.
Ok, is there any way to remove the #! and just end up with '/account' if not it is ok this way.
Yeah, there is, as I explained in my answer below. Use window.location.hash.substring(2), which will take the #! off of the front.
what it give is totally a string, so you can use: location.hash.substring(2); and you should tell if the string's length is > 2, or it would throw an exception.
1

The window.location object has a hash property which contains everything after (and including) the has symbol. To get everything after the #, take off the leading hash symbol:

window.location.hash.substring(1)

2 Comments

This would give me /account/?
No this would give !/account/ If you wanted to remove the exclamation point, change the 1 to 2. This removes the subsequent character.
1

Also consider taking a look at some JavaScript routing libraries like,

  • Crossroads.js (only routing)
  • Backbone.js (provides general client-side app structure which includes a helpful router with pushState support)

Comments

1

You might try taking a look at the Crossroads and Hasher libraries. You can put them together to do what I think you're trying to do.

// setup your routes
crossroads.addRoute('newsfeed.php', function() {
    // do some stuff
});

//setup hasher
hasher.initialized.add(crossroads.parse, crossroads); //parse initial hash
hasher.changed.add(crossroads.parse, crossroads); //parse hash changes
hasher.init(); //start listening for history change

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.