Jess Cauchi Jess Cauchi - 11 months ago 59
jQuery Question

how to get jquery to run repeatedly

I currently have a piece of jquery code that looks for a specific URL (with an anchor at the end) and runs a function if it has a match. The code only runs once, if this is the first URL loaded. Is it possible to have the following code running until it has a match?

var url = "";
if (location.href==url){

Answer Source

It only runs the first time, because changing the hash does not fire the DOM ready handler again, it does however fire the hashchange event.

$(window).on('hashchange', function() {
    if ( window.location.hash === '#backup-section-3' ) {
}).trigger('hashchange'); // fire on first load as well

Note that the window is always available, and does not need a DOM ready handler