Javascript jQuery历史状态和重新加载问题

Javascript jQuery历史状态和重新加载问题,javascript,jquery,html,cordova,Javascript,Jquery,Html,Cordova,我正在使用HTML5的历史。在单页应用程序中按下状态以使用浏览器后退按钮(我使用的是cordova)。所以我尝试在div容器中动态加载内容 根据此示例,我有一个历史堆栈:- (在下面的评论中,有人有同样的问题:- ) 我的html内容是独立的html文件 在我按下f5或浏览器重新加载/刷新按钮之前,一切正常。在重新加载页面时,我得到了一个404,因为浏览器尝试调用假的url,但实际上并不存在。 似乎浏览器希望在自己而不是index.html中加载子页面。 在上面的示例中,作者在html文件中没

我正在使用HTML5的历史。在单页应用程序中按下状态以使用浏览器后退按钮(我使用的是cordova)。所以我尝试在div容器中动态加载内容

根据此示例,我有一个历史堆栈:-

(在下面的评论中,有人有同样的问题:- )

我的html内容是独立的html文件

在我按下f5或浏览器重新加载/刷新按钮之前,一切正常。在重新加载页面时,我得到了一个404,因为浏览器尝试调用假的url,但实际上并不存在。 似乎浏览器希望在自己而不是index.html中加载子页面。 在上面的示例中,作者在html文件中没有动态加载的内容。他从某处收到html代码作为响应,我不知道他是如何做到这一点的

因此,我希望重新加载我的页面,并使单独页面中的内容仍位于index.html中的指定区域

  <li><a id="sp_termine">&Uuml;bersicht der Termine</a></li>
  <li><a id="sp_termine_aendern">Termin &auml;ndern</a></li>
这是我的密码: Index.js:

   $(function () {

            var load = function (url) {
                $.get(url).done(function (data) {
                    $("#content").html(data);
                })
            };


            $(document).on('click', 'a', function (e) {

                    e.preventDefault();
                    console.log(history);
                    var $this = $(this),
                        url = $this.attr("id"),
                        title = $this.text();


                    history.pushState({
                        url: url,
                        title: title
                    }, title, url);

                    document.title = title;

                        //here you can see the folder structure
                        load("/pages/" + url + "/" + url + ".html");



                });


            $(window).on('popstate', function (e) {

                var state = e.originalEvent.state;
                if (state !== null) {
                    document.title = state.title;
                    load(state.url);

                }
                else {
                    document.title = 'example title';

                }
            });


        });
index.html:


来自另一个html文件的链接(主菜单也从index.html加载到contentdiv中)

  • Ü;终点站
  • termä;安德伦
  • 我真的很期待得到一些帮助

    谢谢你。解决方案:

    不要伪造指向不存在位置的URL。这简直是糟糕的风格。使用查询(?)或#:

    简单地做:

    window.location="#mycoolsubsite";
    
    如果重新加载页面,您可能需要检查url是否指向子网站:

    alert(window.location.split("#")[1] || "main"); //will print "main" or if you 'redirected' "mycoolsubsite"
    
    2。解决方案:


    学习php或nodejs并实现路由器。

    请发布您遇到问题的代码,以便我们了解您使用的是什么。
    window.location="#mycoolsubsite";
    
    alert(window.location.split("#")[1] || "main"); //will print "main" or if you 'redirected' "mycoolsubsite"