From 这个问题(How to get request url in a jQuery $.get/ajax request) https://stackoverflow.com/q/3828104/340760我发现我可以用以下方法检索它this.url
.
From MDC 窗口历史记录 https://developer.mozilla.org/en/DOM/window.history我找到了语法并来自MDC 操作浏览器历史记录 https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history我发现我可以通过以下方式获取当前状态history.state
(不过似乎在 Chrome 上不起作用)并发现在调用时pushState
我只能发送 640k 个字符stateObject
否则它会抛出异常。
我当前成功的ajax请求的方法是:
OnSuccess: function(html, status, xhr){
var requestedUrl = this.url.replace(/[&?]X-Requested-With=XMLHttpRequest/i, "");
// if the url is the same, replace the state
if (window.location.href == requestedUrl)
{
history.replaceState({html:html}, document.title, requestedUrl);
}
else
{
history.pushState({html:html}, document.title, requestedUrl);
}
},
对于基本测试,我将整个结果存储在stateObject
,如果它抛出异常,我将设置 URL,以便能够发出新请求。
My popstate
事件是
$(window).bind("popstate", function (e) {
var state = e.originalEvent.state;
$("#body").html(state.html);
});
它会将页面恢复为以前的样子,我不需要发出新的请求。