44254

How do I know if the user clicks the “back” button?

I'm using anchors for dealing with unique urls for an ajaxy website. However, I want to reload the content when the user hits the Browser's "back" button so the contents always matches the url.

How can I achieve this? Is there a jQuery event triggering when user clicks "Back"?

Answer1:

You need to use a hashchange event. There are a couple of popular plugins. I've been using the address plugin and like it. Some will suggest BBQ also.

Address plugin

jQuery BBQ

Answer2:

If you're using ajax urls and not reloading the page, I'm guessing you have #something style URLs, using hash navigation. If that's the case you probably want the window.onhashchange event to reload the appropriate content.

Since it's not supported by all browsers, there's a plugin for this, your code would look something like this:

$(window).hashchange(function() { //do something with location.hash that just changed });

Answer3:

YUI history manager does this by checking the hash every 10ms using setInterval. There is no event registered by back, so that is probably the only way possible.

Recommend

  • $(…).autocomplete is not a function
  • How to force Template.dynamic to re render
  • How to detect URL changes in SPA
  • Define a route based on the path not on the hash value
  • Get the elements of HTML tags inside data-content of popover
  • How do you download a image/png data URI in Edge?
  • ng-pattern that allows decimal ratios
  • prevent datepicker from triggering parent mouseleave
  • JQM (jQueryMobile) Problem with AJAX content and focus()
  • Entity Framework ObjectContext: Concurrency
  • python: forcing relative imports to search from script file
  • EntLib Way to Bind “Null” Value to Parameter
  • Using MouseListener to select a range of cells in a grid
  • Jenkins Grails plugin does not list lastest versions of Grails
  • Iron Router: How do I send data to the layout?
  • Dart - Isolate Cross Window Communication
  • Redshift Querying: error xx000 disk full redshift
  • Django model inheritance, filtering models
  • How to generate and display a QR Code in ionic 2
  • d3 v4 drag and drop with TypeScript
  • Rails Find when some params will be blank
  • JQuery Internet Explorer and ajaxstop
  • Meteor: Do Something On Email Verification Confirmation
  • Cannot resolve symbol 'MyApi'
  • How to get address from latitude and longitude android google map v2 [duplicate]
  • Deselecting radio buttons while keeping the View Model in synch
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Is possible to count alias result on mysql
  • jQuery tmpl and DataLink beta
  • How can I estimate amount of memory left with calling System.gc()?
  • Circular dependency while pushing http interceptor
  • How to disable jQuery.jplayer autoplay?
  • AngularJs get employee from factory
  • Benchmarking RAM performance - UWP and C#
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • using HTMLImports.whenReady not working in chrome
  • How to set the response of a form post action to a iframe source?
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Change div Background jquery
  • Linking SubReports Without LinkChild/LinkMaster