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?

James Hill
  • 60,353
  • 20
  • 145
  • 161
subhojit777
  • 586
  • 3
  • 11
  • 28

2 Answers2

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.

Community
  • 1
  • 1
James Hill
  • 60,353
  • 20
  • 145
  • 161
0
var hash = window.location.hash;
var loc = window.location.href+hash;
window.location=loc;
The Alpha
  • 143,660
  • 29
  • 287
  • 307