为什么后退按钮只使用 OnPopState 更改一次 URL?

why back button only change url once with onpopstate?

提问人:saleh 提问时间:12/18/2022 更新时间:12/18/2022 访问量:25

问:

我使用此功能触发后退按钮点击。 每次我单击后退按钮 onpopstate 触发时,都会使用此代码,但只有当它更改 url 时,当我对此发表评论时,问题就解决了。

   $(document).ready(function() {
        window.onpopstate = function () {
            var urlmethod = get_url_method();
            console.log(urlmethod);
            if (urlmethod == "products") {
                show_main();
            } else if (urlmethod == "cart") {
                show_carts();
            } else if (urlmethod == "orders") {
                orders_click();
            }
        }
    });

我用以下代码更改了代码中的网址:

  function set_url(parent_id){
        const nextState = { additionalInformation: parent_id };
        if (parent_id==0){
            window.history.pushState(nextState, "", "http://localhost");
        }else {
            var parent = data.find(item => item.term_id == parent_id);
            window.history.pushState(nextState, "", "http://localhost/brands/" + parent_id);
        }
    }

我应该改变什么?

javascript pushstate html5-history

评论


答: 暂无答案