Reputation: 596
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?
Upvotes: 3
Views: 1818
Reputation: 61812
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.
Upvotes: 7
Reputation: 146191
var hash = window.location.hash;
var loc = window.location.href+hash;
window.location=loc;
Upvotes: 0