Javascript 有没有办法使用 Jquery 检测跨浏览器按下的后退按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10462511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:25:07  来源:igfitidea点击:

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

javascriptjquery

提问by user516883

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.

我有一个幻灯片放映的网站,当用户按下后退按钮时,我希望它返回到相册视图而不是之前的并阻止页面。有没有办法做到这一点?感谢您的任何帮助或建议。

回答by MUG4N

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

jQuery Address 为浏览器历史记录和 Ajax 爬取提供了强大的跨浏览器支持:

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

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

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

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

Greetings

你好

UPDATE:

更新:

you can find a good sample right here:

你可以在这里找到一个很好的样本:

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

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++);
        }
    });
 });?

回答by Pavel Strakhov

You should:

你应该:

  1. Set location.hashwhen page state changes.
  2. When location.hashchanged by user (for example, by pressing back button) render appropriate page contents.
  1. location.hash当页面状态改变时设置。
  2. location.hash用户更改时(例如,通过按后退按钮)呈现适当的页面内容。