【发布时间】:2012-10-10 08:14:14
【问题描述】:
我已经从https://github.com/browserstate/history.js 下载了 zip,然后在我的 Web 应用程序中上传了 browserstate-history.js-e84ad00\scripts\uncompressed\history.js。我正在使用 html 来测试它。以下是保存在html中的脚本。
<script type="text/javascript">
var count=0;
$(document).ready(function(){
$("#change").click(function(){
count=count+1;
$.ajax({
url: "fetch.html",
cache: false,
dataType: "html",
success: function(responseHTML){
wrappedResponseHTML = "<div id='responseTextWrapperDiv'>" + responseHTML + "</div>";
$("#data").html(($(wrappedResponseHTML).find("#toggle")).html());
history.pushState(document.getElementById("data").innerHTML, "", "?page="+count,"");
}
});
});
$("#change2").click(function(){
count=count+1;
$.ajax({
url: "fetch2.html",
cache: false,
dataType: "html",
success: function(responseHTML){
wrappedResponseHTML = "<div id='responseTextWrapperDiv'>" + responseHTML + "</div>";
$("#data").html(($(wrappedResponseHTML).find("#toggle")).html());
history.pushState(document.getElementById("data").innerHTML, "", "?page="+count,"");
}
});
});
});
window.onpopstate = function(event) {
document.getElementById('data').innerHTML=event.state;
if(event.state==null)
{
window.location.href=window.location.href;
}
};</script>
这是身体部分:
<body>
<div id="data">
Landing Page
</div>
<div>
<input type="button" id="change" value="change text" />
<input type="button" id="change2" value="change text 2" />
</div>
<div>
<input type="button" id="back" value="go back" onclick="history.go(-1);"/>
</div></body>
id 为“data”的 div 中的文本是使用 ajax 更改的。这在 Mozilla 15.0.1 中运行良好,但是当我在 IE 8 中对其进行测试时,历史记录功能不起作用,我不会回到以前的状态。相反,我将返回调用我的 html 的上一页。在其中一个论坛中,建议的解决方案是包含我已经包含的 History.js。我还有什么遗漏的吗?
【问题讨论】:
标签: javascript jquery ajax history.js