HTML5/jQuery:pushState 和 popState - 深度链接?

发布于 2024-12-25 16:01:19 字数 1336 浏览 3 评论 0原文

首先,我似乎无法弄清楚 PushState 函数中的第一个参数的用途是什么?我要传递什么给它?我只是想在滚动页面时更改网址。我正在查询视口中当前元素的 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);

...

$(window).bind('popstate', function(event){
        console.log(event.data);
    });

这始终为空。这不应该返回“测试”吗?

first off I can't seem to figure what the first parameter in the pushState function is for? What do I pass to it? I simply want to change the url when scrolling through my page. I'm querying the ID of the current element in the viewport and its ID should also be the link in the url. That works fine with the code below.

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;

        });

    }

Now I have two questions:

1.) Right now the url in the addressbar changes successfully when I scroll through my page. How can I query the url/hash in the addressbar when I initially load the page. So imagine I have now a link like www.url.com/deep I want to find out what /deep is? Do I simply have to query the entire top.location and split it on each "/"? I mean those links are actually not existing, so how do I avoid 404 pages when calling a url that I manipulated with the pushState function?

2.) How can I find out the last change in the addressbar when clicking the back button? So I want to find /deep when clicking on the browser back button so I can navigate back to that position on the page. I guess this is probably working with popstate but I couldn't find out how!

Thank you for your help!

Update:

window.history.pushState("test", hash, url);

$(window).bind('popstate', function(event){
        console.log(event.data);
    });

This is alway null. Shouldn't this return "test"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

豆芽 2025-01-01 16:01:19

pushState函数中的第一个参数是做什么用的?

来自文档

state 对象 — 状态对象是一个 JavaScript 对象,与 PushState() 创建的新历史记录条目相关联。每当用户导航到新状态时,就会触发 popstate 事件,并且该事件的 state 属性包含历史记录条目的状态对象的副本。

因此,当您返回到该状态时,您将获得一组由您选择的数据。

现在,当我滚动页面时,地址栏中的 URL 会成功更改。当我最初加载页面时,如何查询地址栏中的 url/hash。

查看 location 对象……但您不需要这样做。您可以(并且应该)填充页面服务器端。这是pushState的优点之一,如果JavaScript不可用并且服务器端回退顺利集成,链接仍然有效。

单击后退按钮时如何找出地址栏中的最后一次更改?

您添加一个事件侦听器(查找 popState 事件),并且您存储在其中的数据将在事件对象上可用。 MDN 有一个示例

what the first parameter in the pushState function is for?

From the documentation

state object — The state object is a JavaScript object which is associated with the new history entry created by pushState(). Whenever the user navigates to the new state, a popstate event is fired, and the state property of the event contains a copy of the history entry's state object.

So it is a bundle of data of your choice that you get back when you return to that state.

Right now the url in the addressbar changes successfully when I scroll through my page. How can I query the url/hash in the addressbar when I initially load the page.

Look at the location object … but you don't need to. You can (and should) populate the page server side. This is one of the advantages of pushState, the link still works if JavaScript is not available and the server side fallback is smoothly integrated.

How can I find out the last change in the addressbar when clicking the back button?

You add an event listener (looking for a popState event), and the data you stored in it will be available on the event object. MDN has an example

情感失落者 2025-01-01 16:01:19

jQuery抽象出事件对象,你想要:event.originalEvent.state;

jQuery abstracts out the event object, you want: event.originalEvent.state;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文