首先,我似乎无法确定pushState函数中的第一个参数是什么?我通过了什么?我只是想在浏览我的页面时更改网址。我查询视口中当前元素的ID,其ID也应该是URL中的链接。这与以下代码正常工作。HTML5/jQuery:pushState和popState - 深层链接?
var currentHash,
url;
if (history && history.pushState) {
$(window).scroll(function() {
hash = $('.layer:in-viewport').attr('id');
catHash = $("#"+hash).parent('section').attr('id');
var data = "nothing";
if (catHash != undefined)
url = "/" + catHash + "/" + hash;
else
url = "/" + hash;
if (currentHash != hash) {
window.history.pushState(data, hash, url);
}
currentHash = hash;
});
}
现在我有两个问题:
1)现在在地址栏的网址成功地改变了,当我通过我的页面滚动。我最初加载页面时如何查询地址栏中的url/hash。所以想象我现在有一个链接,如www.url.com/deep
我想找出什么/深是什么?我是否只需要查询整个top.location并将其分割为每个“/”?我的意思是这些链接实际上不存在,那么当我调用一个使用pushState函数操作的url时,如何避免404页面?
2.)单击后退按钮时,如何才能找出地址栏中的最后更改?所以我想在点击浏览器后退按钮时找到/deep
,这样我就可以导航到页面上的那个位置。我想这可能与popstate一起工作,但我找不到如何!
谢谢你的帮助!
更新:
window.history.pushState("test", hash, url);
...
$(window).bind('popstate', function(event){
console.log(event.data);
});
这常是空。不应该返回“测试”吗?
'event.state'不'event.data' – Quentin 2012-01-08 10:12:04
是的,我试过了,但是没有'event.state'当我'控制台.log(event);'只有数据,但是数据总是“空”,如果我尝试记录'event.state',它总是空的! – matt 2012-01-08 10:17:14
好吧,找到解决方案......好像'$(window).bind('popstate',function(event){'不工作,但是'window.onpopstate = function(event){'does! – matt 2012-01-08 11:16:04