I'd like to execute a javascript (or jQuery) initialization function -- let's call it myPageInit()
after a page is fully loaded.
Here's the catch: myPageInit()
should run only when the page is initially loaded or if the user forces a page refresh (i.e. presses the browser's reload button, hits F5
, etc.).
It should not run if the user follows a link from the page and then hits the back button.
For the avoidance of doubt:
- User navigates to
www.mypage.com
=>myPageInit()
runs after page is loaded. - User now hits browser refresh button =>
myPageInit()
runs after page is reloaded. - User now clicks on a link on the page (e.g.
<a href="www.cnn.com">...
). - User clicks back button to get back to
www.mypage.com
=>myPageInit()
does not run after page is shown.
What I've tried so far
$(window).load()
-- I think this used to work, but on modern browsers this appears to fire when the user presses the back button (see the comments on this issue)$(window).ready()
-- This also appears to fire on both initial load and back button.Searching SO for other relevant answers. I still haven't found what I'm looking for.
Thanks in advance!