简体   繁体   English

Ajax 调用上的 pushState?

[英]pushState on Ajax call?

My client is tormenting me to be able to go back to a 'previous page' using the browser back button.我的客户正在折磨我,让我能够使用浏览器后退按钮返回“上一页”。 The thing is that this 'pages' are being called via Ajax to a modal window that displays the content.问题是这个“页面”通过 Ajax 被调用到显示内容的模式窗口。 I'm doing an ajax call and I found that pushState will be my solution, but I really don't get it.我正在执行 ajax 调用,我发现 pushState 将是我的解决方案,但我真的不明白。 I found stuff where there's not even a bit of ajax, it's all javascript.. So, what should I do to add pushState to an ajax call?我发现了一些甚至没有一点 ajax 的东西,都是 javascript.. 那么,我应该怎么做才能将 pushState 添加到 ajax 调用中? Is that even possible?这甚至可能吗? Or should I just a find a way to make it work with Ajax?或者我应该只是想办法让它与 Ajax 一起工作?

I found this thing called Pjax but I really don't get it.我发现了一个叫做 Pjax 的东西,但我真的不明白。 My ajax call looks something like this;我的 ajax 调用看起来像这样;

$(function() {
    $('.w-container .w-nav-menu a').click(function() {
        var $linkClicked = $(this).attr('href');
        var $pageRoot = $linkClicked.replace('#', '');
        if (!$(this).hasClass("active")) {
            $(".w-container .w-nav-menu a").removeClass("active");
            $(this).addClass("active");
            $.ajax({
                type: "POST",
                url: "./PATH/load.php",
                data: 'page='+$pageRoot,
                dataType: "html",

                beforeSend: function(){
                        $('#canvasloader-container.wrapper').show();
                    },
                complete: function(){
                        $('#canvasloader-container.wrapper').hide();
                    },                
                success: function(msg){
                    if((msg))
                    {
                        $('.content').html(msg);
                        $('.content').hide().fadeIn();
                    }
                }

            });
        }
    event.preventDefault();
});

I'm sorry if someone else already created something like this, but I didn't find anything useful如果其他人已经创建了这样的东西,我很抱歉,但我没有发现任何有用的东西

You can do a pushState at any stage you'd like in the code you provided, depending on when do you want the history to be manipulated.您可以在您提供的代码中的任何阶段执行pushState ,具体取决于您希望何时操作历史记录。

You can just do history.pushState([data], [title], [url]);你可以只做history.pushState([data], [title], [url]); (with appropriate values as parameters) just after a successful ajax form return and before replacing the html content like this: (使用适当的值作为参数)就在成功返回 ajax 表单之后和替换 html 内容之前,如下所示:

success: function(msg){
    if((msg))
    {
        history.pushState([data], [title], [url]); // replace with appropriate values as parameters
        $('.content').html(msg);
        $('.content').hide().fadeIn();
    }
}

There might be an issue depending on the UX you've set up, if a user is accessing pages continuously by ajax calls and there are no URLs to reflect the previous page or 'state' in that case, then this solution probably won't work for you.根据您设置的用户体验,可能存在问题,如果用户通过 ajax 调用连续访问页面,并且在这种情况下没有反映上一页或“状态”的 URL,则此解决方案可能不会为你工作。

History manipulation functions like pushState only change the browser history, they don't control what happen when someone clicks the 'back' button.pushState这样的历史操作函数只改变浏览器历史,它们不控制当有人点击“后退”按钮时会发生什么。

If there are no pages being loaded with URLs in your setup then you might need to control what happens when a user clicks the 'back' button, by doing another ajax call and loading the previous content (you got to keep track of the changes in content).如果在您的设置中没有加载带有 URL 的页面,那么您可能需要通过执行另一个 ajax 调用并加载以前的内容来控制用户单击“后退”按钮时发生的情况(您必须跟踪内容)。 If that is your scenario, this might help you getting started: https://stackoverflow.com/a/25806609/4283725如果这是您的情况,这可能会帮助您入门: https : //stackoverflow.com/a/25806609/4283725

The best solution is I think use a javascript component that handles this for you.最好的解决方案是我认为使用一个 javascript 组件来为你处理这个问题。 I use for example Barba.js.我使用例如 Barba.js。 Thats a component that used PJAX.那是一个使用 PJAX 的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM