3

I have an image, on clicking the image the URL changes but the page is not reloaded(partial navigation). I have used window.location.href that fetches the current URL, but it displays the previous URL on console log. I want to fetch the URL after it changes.

Am I missing some window wait event?

2
  • When you say partial navigation, is some type of AJAX occurring, or are you simply changing the hash of the url?
    – James Hill
    Commented Apr 20, 2012 at 12:20
  • The URL remains the same just the # changes Commented Apr 20, 2012 at 12:21

2 Answers 2

7

To retrieve the new hash of the page, use location.hash:

var hash = window.location.hash;

For a similar requirement in the past, I've used Ben Alman's hashChange plugin. Once the plugin is included on page, you can attach code to the hashChange event:

$(window).hashchange( function(){
    // Your code here
})

Here's a working fiddle to demonstrate.

Additional Information

This SO post is worth reading: On - window.location.hash - change?

Note

If you don't want to use a plugin, you'll have to post your markup before I can provide an alternative solution.

3
  • but it will fetch the hash of the previous URL. my hash changes every time i click on different image. i have to fetch the URL after it gets changed by click event. Commented Apr 20, 2012 at 12:27
  • 1
    @subhojit777, I've updated my answer to reflect your concerns. Will this meet your needs?
    – James Hill
    Commented Apr 20, 2012 at 13:00
  • thanks for the last link. i knew that there was some problem with event. Commented Apr 20, 2012 at 13:07
0
var hash = window.location.hash;
var loc = window.location.href+hash;
window.location=loc;

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.