Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way using Jquery to detect the back button being pressed cross browsers

I have a website that is on a slide show and when the user presses the back button I would like it to go back to the album view not the prior and prevent page. Is there a way of doing this? thanks for any help or advice.

like image 975
user516883 Avatar asked May 05 '12 13:05

user516883


2 Answers

jQuery Address provides strong cross-browser support for browser history and Ajax crawling:

http://www.asual.com/jquery/address

http://github.com/asual/jquery-address

Greetings

UPDATE:

you can find a good sample right here:

http://jsfiddle.net/5L6Ur/10/

some code in short:

 $(function() {
    $('a').click(function(e) {
        e.preventDefault();
        $.address.value($(this).attr('href'));
    });
    var changecount = 0;
    $.address.change(function(e) {
        if (typeof e !== 'function') {
            $('span').html(changecount++);
        }
    });
 });​
like image 98
MUG4N Avatar answered Oct 14 '22 20:10

MUG4N


You should:

  1. Set location.hash when page state changes.
  2. When location.hash changed by user (for example, by pressing back button) render appropriate page contents.
like image 22
Pavel Strakhov Avatar answered Oct 14 '22 21:10

Pavel Strakhov