There's no event in jQuery where it detects the url changing or whatsoever. But, I've come across this once and I used LocalStorage to solve my issue. Here's how my code mostly looks like.
E.g.
Page 1
$(document).ready(function(){
// Check if browser supports LocalStorage
if(typeof(Storage) !== 'undefined') {
var origin = document.location.origin; // e.g. "http://stackoverflow.com"
var pathname = document.location.pathname; // e.g. "/questions/36975059/using-jquery-to-get-current-url-path-name-and-detect-if-changes"
localStorage.setItem('origin', origin);
localStorage.setItem('pathname', pathname);
}
});
Page 2 (Or wherever you want to detect if it changes)
$(document).ready(function(){
// Check if browser supports LocalStorage
if(typeof(Storage) !== 'undefined') {
var origin = document.location.origin;
var pathname = document.location.pathname;
// Check if it changes here
var storageOrigin = localStorage.getItem('origin');
var storagePathname = localStorage.getItem('pathname');
// Check if storageOrigin and storagePathname are not null or undefined.
if (storageOrigin && storagePathname) {
if (storageOrigin !== origin || storagePathname !== pathname) {
// The previous origin and pathname are not the same. Do your things here.
}
else {
// They're like the same.
}
}
localStorage.setItem('origin', origin);
localStorage.setItem('pathname', pathname);
}
});
Hope it helps..