Note that there are some explanatory texts on larger screens.

plurals
  1. POAlternative to jQuery ready() for detecting that the page anchor in the URL changes
    text
    copied!<p>Does anyone know how to get a page load event to fire if the page anchor in the URL changes, i.e., going from </p> <pre><code>http://mywebsite.com/#test1 to http://mywebsite.com/#test2 </code></pre> <p>when using the Back button?</p> <p>I tried the code at the bottom (the <code>history.navigationMode</code> line along with the <code>ready()</code> function insures that the onload event fires even when the browsers Back button is used), but it doesn't work if only the page anchor portion of the URL changes and the Back button is used. I know this code works if the URL changes it's query string (or the URL points to a new file/folder) because I use this all the time but it looks like jQuery's ready() function doesn't fire when the Back button is used if only the page anchor location in the URL changes.</p> <p>I'm trying to come up with a solution for making the browser's Back button preserve JavaScript/AJAX manipulations of the page and I'm basing my attempt on this person's idea: <a href="http://www.ajaxonomy.com/2008/web-design/a-better-ajax-back-button-part2" rel="nofollow noreferrer">http://www.ajaxonomy.com/2008/web-design/a-better-ajax-back-button-part2</a> but without relying on using a repeating timer to check if the URL page anchor has changed - I would rather check this when the page onload event fires.</p> <p>Any ideas/suggestions? </p> <pre><code>&lt;script language="javascript" type="text/javascript" src="/jquery.js"&gt;&lt;/script&gt; &lt;script language="javascript" type="text/javascript"&gt; history.navigationMode = 'compatible'; $(document).ready( function() { if(document.location.hash){ var hashQueryString = document.location.hash; hashQueryString = hashQueryString.replace("#",""); alert(hashQueryString); } }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload