Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if the user clicked the "back" button

When the user goes history-back-1...how do I detect that? And then, alert "the user clicked back!"

Using binds (and jQuery preferably)

like image 558
TIMEX Avatar asked Jan 05 '10 20:01

TIMEX


People also ask

What event is fired when the back button of a browser is pressed?

The popstate event is fired each time when the current history entry changes (user navigates to a new state). That happens when user clicks on browser's Back/Forward buttons or when history. back() , history.


1 Answers

You generally can't (browser security restriction). You can tell if the user navigates away from the page (onbeforeunload, onunload fire) but you can't tell where they went unless you've set up your page to allow it.

HTML5 introduces the HTML5 History API; in conforming browsers, the onpopstate event will fire if the user navigates back to an earlier "page" on your site.

like image 72
EricLaw Avatar answered Oct 05 '22 06:10

EricLaw