首先,我似乎无法弄清楚 pushState 函数中的第一个参数是干什么用的?我要传递给它什么?我只是想在滚动页面时更改 url。我正在查询视口中当前元素的 ID,它的 ID 也应该是 url 中的链接。这适用于下面的代码。
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 会成功更改。最初加载页面时,如何在地址栏中查询 url/hash。所以想象一下我现在有一个链接,比如www.url.com/deep
我想找出 /deep 是什么?我是否只需要查询整个 top.location 并将其拆分为每个“/”?我的意思是这些链接实际上并不存在,那么在调用我使用 pushState 函数操作的 url 时如何避免 404 页面?
2.) 单击返回按钮时,如何查看地址栏中的最后一次更改?所以我想/deep
在点击浏览器后退按钮时找到,以便我可以导航回页面上的那个位置。我想这可能与 popstate 一起使用,但我不知道怎么做!
感谢您的帮助!
更新:
window.history.pushState("test", hash, url);
…</p>
$(window).bind('popstate', function(event){
console.log(event.data);
});
这始终为空。这不应该返回“测试”吗?