因此,我使用HTML5历史记录管理功能来添加在带有AJAX加载的子内容的网站中前后导航的功能。如何在HTML5历史记录状态下存储功能
现在我想将状态对象中的javascript函数存储到弹出状态的回调函数中。或多或少像下面的代码:
$(window).bind("popstate", function(event) {
var state = event.originalEvent.state;
if (!state) {
return;
}
state.callback(state.argument);
}
function beforeLoad() {
var resourceId = "xyz";
var func;
if (case1) {
func = switchPageToMode1;
} else { // case 2
func = swithPageToMode2;
}
func(resourceId); // run the function
window.history.pushState({ callback: func, resourceId: resourceId }, "newTitle", "newURL"); // and push it to history
}
function switchPageToMode1(resourceId) {
alterPageLayoutSomeWay();
loadResource(resourceId);
}
function swithPageToMode2(resourceId) {
alterPageLayoutSomeOtherWay();
loadResource(resourceId);
}
function loadResource(resourceId) {
...
}
好的。所以我想要做的是存储一个JavaScript函数的引用。但是推的状态(实际window.history.pushState调用)时,浏览器文件的投诉,即错误:“DATA_CLONE_ERR:DOM异常25”
任何人知道我在做什么错?是否可以在状态中存储函数调用?
非常感谢!这解释了一切;在序列化部分错过了。 – stafffan
@Jordan我存储一个对象,它看起来像'{html:$('#main')。html()}',但即使我使用IE 11,我仍然在IE中收到'Data Clone Error' /或添加一些额外的历史API,如https://github.com/devote/HTML5-History-API/blob/master/history.js。我还能做什么错? –