window.history.pushState不在历史记录中 [英] window.history.pushState not going back in history

查看:447
本文介绍了window.history.pushState不在历史记录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使用history.pushstate事件时遇到了一些问题。我进行了设置,以便页面的url成为通过AJAX加载的页面的实际URL,并且运行正常。

I have a little problem experimenting with the history.pushstate event. I set it up so that the url of the page would be the actual URL of the page loaded via AJAX and that works just fine.

我明白它应该自动创建历史记录,加载以前加载的页面。不幸的是帽子没有发生,当我点击前面时,网址确实会改变而不是页面。
你能帮帮我吗?这是我的简化代码:

I understood that it should automaticaly create a history, loading the pages previously loaded. unfortunatly hat doesn't happen and when i click back of forward the url does change but not the page. Can you help me? Here is my simplified code:

    function hijackLinks() {
        $('a').live("click", function (e) {
            e.preventDefault();
            loadPage(e.target.href);    
            direction = $(this).attr('class');        
        });   
    }


    function loadPage(url) {
        if (url === undefined) {
            $('body').load('url', 'header:first,#content,footer', hijackLinks);
        } else {
            $.get(url, function (data) {
                $('body').append(data);
                 window.history.pushState(url, url, url);

                if (direction === "leftnav") {
                   //DO STUFF
                }
                if (direction !== "leftnav") {
                   //DO STUFF
                }

                setTimeout(function () {
                  //DO STUFF
                },1000);
            });
        }
    }
    $(document).ready(function () {
        loadPage(); 

    });


推荐答案

想出来:D,好吧。至少它有效:)

Figured it out :D, well kind of. At least it works :)

我刚刚添加了

    window.addEventListener("popstate", function(e) {
    loadPage(location.pathname);
});

到页面末尾:))

这篇关于window.history.pushState不在历史记录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆