【发布时间】:2012-12-25 05:41:23
【问题描述】:
我正在通过 Ajax 加载页面。当用户单击链接时,页面正在成功加载 AJAX,但是当用户单击后退按钮时,页面会重新加载初始页面。所以场景是这样的。
- 加载初始页面(index.php)
- 用户点击链接
- 页面加载成功
- 点击后退按钮
- 初始页面现在显示两次。
这是标记。
$(function() {
// 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();
$('#content').load(State.url);
});
$('a').click(function(evt) {
evt.preventDefault();
History.pushState(null, $(this).text(), $(this).attr('href'));
alert(State.url)
});
});
这是标记
<div id="wrap">
<a href="page1.html">Page 1</a>
</div>
<div id="content">
<p>Content within this box is replaced with content from
supporting pages using javascript and AJAX.</p>
</div>
如果你仍然没有得到我的问题或场景
这是完整的场景。 初始页面
当用户点击链接时,所选页面加载成功
当我点击后退按钮时,初始页面现在翻了一番
如您所见,“Page1”链接翻了一番。这是浏览器的问题吗?还是我对历史 API 的理解是缺少或缺失的?对此有什么可能的解决方案?
【问题讨论】:
-
这是你的错误!当您返回时,您将在
<div id="content"></div>内加载整个页面。
标签: javascript jquery ajax history.js