javascript jquery ajax 发布调用,ajaxify 历史记录和后退按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25216956/
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
jquery ajax post call, ajaxify history and back button
提问by Tadej Vengust
I have ajax calls made in jquery function which i call with onClick options placed on divs. example:
我在 jquery 函数中进行了 ajax 调用,我使用放在 div 上的 onClick 选项进行调用。例子:
<div class='basic' onClick='example( <?php echo numberIwant ?> )'> example </div>
and the functions than looks like this:
功能看起来像这样:
function example(ID){
$.ajax({
type: "POST",
url: "example.php",
data: "ID="+ID, success: function(msg){$("#main").html(msg);}
});
}
Now I want to make browser back button to work, to open the previous page(ajax content). I googled and tried multiple scrips like ajaxify and history.js and so on but I just cannot get it working.
现在我想让浏览器的后退按钮工作,打开上一页(ajax 内容)。我用谷歌搜索并尝试了多个脚本,如 ajaxify 和 history.js 等,但我无法让它工作。
I don't know if I either don't know how to use ajaxify properly or if it just doesent work with this kind of method..
我不知道我是否不知道如何正确使用 ajaxify 或者它是否只是不适合这种方法..
Can anyone help me?
谁能帮我?
采纳答案by joeltine
Using the backbutton w/ AJAX has historically been a very common problem. Luckily, with HTML5 came history.pushState which sort of allows you to manually manipulate what the browser does during a navigation (e.g., backbutton).
使用带有 AJAX 的后退按钮历来是一个非常普遍的问题。幸运的是,随着 HTML5 出现了 history.pushState,它允许您手动操作浏览器在导航期间所做的事情(例如,后退按钮)。
Some good resources on this:
一些很好的资源:
http://diveintohtml5.info/history.html
http://diveintohtml5.info/history.html
http://adhockery.blogspot.com/2011/02/javascripts-history-object-pushstate.html
http://adhocery.blogspot.com/2011/02/javascripts-history-object-pushstate.html
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history