鉴于以下内容:
$(window).bind("popstate", function() {
alert('popstate');
});
Run Code Online (Sandbox Code Playgroud)
首次加载时,警报会触发FireFox和Chrome,但不会触发Safari.这是为什么?其他人看过这个并知道如何最好地解决这个问题?
我正在学习HTML5中的历史,在这个例子中(打开JavaScript浏览器控制台以查看错误)event.state.url返回:
Uncaught TypeError: Cannot read property 'url' of undefined
Run Code Online (Sandbox Code Playgroud)
看看和帮助:http://jsfiddle.net/un4Xk/
如何获取我使用HTML5 History API设置的状态对象:
var stateObj = { foo: "bar" };
history.pushState(stateObj, null, url);
Run Code Online (Sandbox Code Playgroud)
我在尝试:
$(window).bind('popstate', function(event) {
alert(event.state);
});
Run Code Online (Sandbox Code Playgroud)
它回来了undefined.
谢谢!
我正在我的页面上工作,但找不到我的问题的答案。
我正在使用 window.history.pushstate 和 onpopstate 事件,这是我的代码:
changeContent = function(href) {
window.history.pushState({}, '', href);
if (href == "/") {
$('#main').load('main.html');
} else {
$('#main').html('<iframe id="gallery" src="photos'+href+'/index.html"></iframe>');
}
};
$(window).on("popstate", function(e) {
changeContent(location.pathname);
});
$(document).on("click", ".galleryLink", function() {
var href = $( this ).attr( "href" );
changeContent(href)
return false;
});
Run Code Online (Sandbox Code Playgroud)
IE
谷歌页面 -> 我的主页 -> 图库 -> 后退按钮 -> 我的主页 -> 后退按钮 -> 我的主页 -> 后退按钮 -> 我的主页
这是怎么回事?