• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

My Monkey Do

A Log of Coding Solutions

  • Home
  • Web Hosts
  • Tools
  • About

Onload Event When Back button is Pressed

May 29, 2011 by Webhead

Problem:

I have some ajax that pulls search results similar to how google images dynamically loads more images using javascript.  So if the user clicks to see ‘page 2’ or ‘more’, javascript runs and fetches more images without the user leaving the page.  If a user clicks on one of the results it will take them to another page.  If the user then presses the Back button, the page that the user was on is lost because it’s as if they are visiting the page for the first time.   A variable can be saved on the page to remember what page the user was on, but the onload javascript functions do not run when the back button is press.

 

Solution:

One of the solutions on stack overflow works like a charm.  The following code lets the “onload” or jquery “ready” functions run when the back button is presssed.

history.navigationMode = 'compatible';
$(document).ready( function(){
    alert('test');
});

 

Filed Under: Coding Tagged With: javascript

Primary Sidebar

Topics

apache apple block editor chrome cms css debug eCommerce embed firebug firefox git gmail goDaddy google hosting htaccess html html 5 IE crap image iPad iPhone javascript jquery linux localization mac os x ms sql mysql open source optimize php php 5.3 responsive rest api seo svg tinymce woocommerce wordpress wpengine xss yii youtube




Categories

  • Coding
  • Off the Shelf
  • Plugins
  • Random Thoughts
  • Server Stuff
  • Tools