How you do this depends on the site / application and what you are trying to do. Here are your options , the simplest and most reliable:
Do not attempt to catch URL changes. Use the waitForKeyElements() calls to work on parts of the various pages that you wanted to manipulate in the first place. This neatly handles the whole time problems inherent in all other approaches.
See Also: "Selecting and Activating the Right Controls on an AJAX-Managed Site . "
Just a poll to change the URL. It is simple and works in practice with fewer time issues than all but 1.
If the site uses AJAX, which modifies the fragment (AKA hash), the hashchange event lights up , Alas, fewer and fewer AJAX sites do this.
Use Mutation Observers to track changes to the <title> . Most AJAX pages are good enough to change the name. This may work before loading targeted content.
Hack history.pushState function. This gives a faster notification of changes to the page, BUT 95% of the time, it works before loading your target elements. You usually need a timer. In addition, it causes cross-region issues in the user interface environment.
For reference , an example survey is provided here . It is still the best cross-browser, boneless, catch-all method:
var fireOnHashChangesToo = true; var pageURLCheckTimer = setInterval ( function () { if ( this.lastPathStr !== location.pathname || this.lastQueryStr !== location.search || (fireOnHashChangesToo && this.lastHashStr !== location.hash) ) { this.lastPathStr = location.pathname; this.lastQueryStr = location.search; this.lastHashStr = location.hash; gmMain (); } } , 111 ); function gmMain () { console.log ('A "New" page has loaded.');
Brock adams
source share