ajax搜索表单结果的history.pushState

时间:2017-01-12 12:47:16

标签: jquery ajax history

我有一个页面,搜索结果通过jquery ajax加载。用户可以单击搜索结果的详细信息页面,然后在详细信息页面上单击后退按钮以获取搜索结果。但是,如果我点击后退按钮(window.history.back),所有搜索结果都将丢失,只显示搜索表单。

我试过window.history.pushState但似乎无法正常工作。希望有人可以帮忙!这是我的代码:

$(document).ready(function() {
                        $("#suchanfrage").on("submit", function() {

                                $("#ima_transparency").fadeIn();
                                $("#loaderImage").fadeIn();

                                $loadajax = $('#suchergebnis');

                                var $mydiv = $('#loadajax_o');
                                //$mydiv.css('height', $mydiv.height() );

                                var $form = $("#suchanfrage");
                                var method = $form.attr("method") ? $form.attr("method").toUpperCase() : "GET";
                                $.ajax({
                                    url: $form.attr("action"),
                                    dataType:'html',
                                    data: $form.serialize(),
                                    type: method,
                                    success: function(data) {
                                        $loadajax.html('');

                                        $("#suchergebnis").html(data);

                                        var newheight = $('#suchergebnis').height();
                                        //$('#loadajax_o').animate( {height: newheight}, function() {
                                            $('html, body').animate({
                                                scrollTop: $('#suchergebnis').offset().top
                                            }, 800);    
                                        //} );

                                        $("#ima_transparency").fadeOut();
                                        $("#loaderImage").fadeOut(function() {
                                            var url = window.location.href; 
                                            window.history.pushState('data', 'Suchergebnisse', url+'#suchergebnis');
                                        });



                                    },
                                    error: function(xhr, status, error) {
                                      alert(xhr.responseText);
                                    }
                                });
                            //window.history.pushState({href: href}, '', href);
                            return false;
                        });

                        window.addEventListener('popstate', function(e){
                          if(e.state)
                            openURL(e.state.href);
                       }); 
                    });

谢谢! 马丁

0 个答案:

没有答案