Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript : Change the function of the browser's back button

Tags:

Is there a way to make the user's back button on their browser, call a javascript function instead of going back a page?

like image 650
cointilt Avatar asked Sep 22 '09 21:09

cointilt


People also ask

How do I go back a button in JavaScript?

You can use the history. back() method to tell the browser to go back to the user's previous page. One way to use this JavaScript is to add it to the onclick event attribute of a button. Here, we create the button using a <form> element, containing an <input> element of the button type.


1 Answers

You can't override the behaviour that if a user follows a link to your page, clicking Back will take them off it again.

But you can make JavaScript actions on your page add entries into the history as though they were clicks to new pages, and control what happens with Back and Forward in the context of those clicks.

There are JavaScript libraries to help with this, with Really Simple History being a popular example.

like image 88
RichieHindle Avatar answered Sep 23 '22 15:09

RichieHindle