让我的后退和前进按钮工作与ajax
getting my back and forwards buttons to work with ajax
多亏了我精心修改的一些代码,我的网站工作得更快了,但我希望浏览器的后退/前进按钮能正常工作。现在,使用下面的代码,浏览器地址栏永远不会改变。当有人点击"返回"时,它会把他们带出应用程序。
是否有任何简单的方法来改变这一点,使浏览器的后退/前进按钮工作?或者有人能给我指出正确的方向。谢谢你的帮助。
$(document).on("ready", function () {
//I want to load content into the '.page-content' class, with ajax
var ajax_loaded = (function (response) {
$(".page-content")
.html($(response).filter(".page-content"));
$(".page-content .ajax").on("click", ajax_load);
});
//the function below is called by links that are described
//with the class 'ajax', or are in the div 'menu'
var history = [];
var ajax_load = (function (e) {
e.preventDefault();
history.push(this);
var url = $(this).attr("href");
var method = $(this).attr("data-method");
$.ajax({
url: url,
type: method,
success: ajax_loaded
});
});
//monitor for clicks from menu div, or with
//the ajax class
//why the trigger?
$("#menu a").on("click", ajax_load);
$(".ajax").on("click", ajax_load);
$("#menu a.main").trigger("click");
});
这是一种检测你在问什么的方法。
听我说,按后退和前进键是一项危险的任务。
window.onload = function () {
if (typeof history.pushState === "function") {
history.pushState("someState", null, null);
window.onpopstate = function () {
history.pushState("newState", null, null);
// Handle the back (or forward) buttons here
// Will not handle refresh, use onbeforeunload for this.
};
}
}
您可以使用jquery地址插件(http://www.asual.com/jquery/address/)。它有一个事件,当用户按下后退/前进按钮时检测。
$.address.externalChange(function() { console.log('back/forward pressed'); });
据我所知,没有办法区分后退和前进
你一定要检查History.js下面是一些示例代码:-
(function(window,undefined){
// Prepare
var History = window.History; // Note: We are using a capital H instead of a lower h
if ( !History.enabled ) {
// History.js is disabled for this browser.
// This is because we can optionally choose to support HTML4 browsers or not.
return false;
}
// Bind to StateChange Event
History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
var State = History.getState(); // Note: We are using History.getState() instead of event.state
//History.log(State.data, State.title, State.url);
var goto_url = State.url;
$.ajax({
url: goto_url,
dataType: "script"
});
});
})(window);
相关文章:
- Sencha Touch使一个简单的按钮工作
- Chrome扩展程序:浏览器操作单击按钮工作一次
- jquery如何让这个对话框的“确定”按钮工作
- 我的设备后退按钮工作不正常,它转到第一个html,但返回到第二个html
- 使“编辑”按钮工作
- 如何使单选按钮工作两个任务
- 通过输入键(不起作用)或搜索按钮(工作)查询结果
- 我有三个地方供用户登录,在同一页面上,我如何在一个功能中让所有三个按钮工作和三个表单处理
- 如果 ID 消失,提交按钮工作
- 无法让Gmail一键确认操作按钮工作
- 我无法使这些按钮工作
- 如何让我的离子侧菜单按钮工作
- 试图让按钮工作的功能,但我的警报弹出了书面脚本
- 一个按钮工作,其他不,相同的脚本
- 使用jquery ajax和PHP的浏览器“后退按钮”工作
- 在Firefox上禁用JavaScript意味着我的按钮不能工作,是否有一种方法可以让按钮工作?
- 按钮onclick按钮工作奇怪
- 在这种情况下,我如何使load more按钮工作
- 让我的后退和前进按钮工作与ajax
- 当在主干中使用推送状态重定向时,我如何保持“返回”?按钮工作