How can I trigger on URL change in jQuery?

How can I trigger a function when the URL changes? I try something like:

   //execute code

So my URL is something like How can I execute jQuery or JavaScript code when the URL becomes something like

That would be a hashchange event, so I’d suggest:

$(window).on('hashchange', function(e){
    // do something...

JS Fiddle demo.


Try the hashchange event, which is built exactly for this –

var current_href = location.href;
    if(current_href !== location.href){
        // if changed Do ...
        current_href = location.href;
        // Do ...

This worked for me ^^

$(window).on(‘hashchange’) // not firing

You could also try using


    $.address.internalChange(function(e) {
        // do something here

To Detect URL change only for pop use

window.onpopstate = function (event) {
//enter code here

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   How can I display a 'Reload' symbol in HTML without loading an image via HTTP?

Similar Posts