简体   繁体   English

在页面加载之前先加载页面内容加载JQUERY Mobile

[英]Load Page content before page Load JQUERY Mobile

I found lots of questions/answers regarding how to dynamically load page content into a div, however, I can't get mine to load. 我发现了很多有关如何将页面内容动态加载到div中的问题/答案,但是我无法加载。

Basically I'm creating a webapp where upon clicking a button on the home page (index) it would call a second page (check.html). 基本上,我正在创建一个webapp,单击主页(索引)上的一个按钮后,它将调用第二个页面(check.html)。 Before that page is loaded, I'm doing an ajax call to the server which returns a json object, then I'm generating a table with that object. 在加载该页面之前,我正在对返回一个json对象的服务器进行ajax调用,然后使用该对象生成一个表。 (please see the code below. (请参见下面的代码。

Then problem is, when I click, I get to the page (check.html) but the content was not loaded, then when I do a refresh everything is then loaded. 然后的问题是,当我单击时,我转到页面(check.html),但是内容没有加载,然后刷新时,所有内容都加载了。

I know it was to do with loading the data before the page is shown, but I can't get it to work. 我知道这与在显示页面之前加载数据有关,但是我无法使其正常工作。 I've tried so many jquery functions (on document ready, on load, and none is working). 我已经尝试了很多jquery函数(在准备好文档,加载时,没有一个起作用)。

Any help woule be appreciated. 任何帮助将不胜感激。

$(document).on('pagebeforeshow', '#checkPage', function(){ 

    //validate if the user has entered/save his email address
    if (localStorage.getItem("email") === null){
      $('#noEmail').show();
      $('#issues').hide();
    }else{
      $('#noEmail').hide();
      $('#issues').show();

      var email = localStorage.getItem("email");

        //ajax call to a server to get the json data
        $.ajax({
            url: 'http://landlord.tenantconnectapp.co.uk/webapp/issues.php',
            type: 'POST',
            data: {email: email},

            beforeSend: function() {
                $.mobile.showPageLoadingMsg(true); // This will show ajax spinner
            },
            complete: function() {
                $.mobile.hidePageLoadingMsg(); // This will hide ajax spinner
            },
            success: function (result) {
                var data = $.parseJSON(result)
                var count = data.length;
                $("#count").append(count);

                var tbl_body = "";
                $.each(data, function (index, issue) {
                  var tbl_row = "";
                  tbl_row += '<th><a href="issue.html" onclick="sessionStorage.ID='+issue['id']+'">'+issue['id']+'</a></th>';
                  tbl_row += "<td>"+issue['createdDate']+"</td>";
                  tbl_row += "<td>"+issue['status']+"</td>";
                  tbl_row += "<td>"+issue['reason']+"</td>";
                  tbl_row += "<td>"+issue['summary']+"</td>";
                  tbl_body += "<tr>"+tbl_row+"</tr>";
                });  
                $("#issuesTable tbody").append(tbl_body);
                $("#issuesTable").trigger("create");
                $("#issuesTable").table("refresh");
                },
            error: function (request,error) {            
                toast('Network error has occurred please try again!');
            }
        });

    }
  });

This is a wild shot but I think I'm right. 这是一个疯狂的镜头,但我认为我是对的。

You didn't mentioned this but I would bet this AJAX call from your second page is placed inside a HEAD or at the end of your file? 您没有提到这一点,但是我敢打赌您第二个页面中的AJAX调用是放在HEAD内还是文件结尾?

If any of those two is correct then you are doing this in the wrong way. 如果这两个中的任何一个是正确的,那么您这样做的方式是错误的。

In few words, when jQuery Mobile starts it will load initial HTML page to the DOM. 简而言之,当jQuery Mobile启动时,它将初始HTML页面加载到DOM。 Because AJAX is used for page handling, when opening subsequent pages, jQuery Mobile will load ONLY data-role="page" div content, everything else is going to be discarded, including HEAD. 由于AJAX用于页面处理,因此当打开后续页面时,jQuery Mobile将仅加载data-role =“ page” div内容,所有其他内容(包括HEAD)都将被丢弃。 One last thing, only ONE page is going to be loaded. 最后一件事,将只加载一页。 If you have more then one data-role="page" in your subsequent pages, every page, except first one will get discarded just like HEAD. 如果随后的页面中有一个以上data-role =“ page”,则除第一个页面外的每个页面都将像HEAD一样被丢弃。

When you initiate a refresh that page will not fully load into the DOM thus initializing JavaScript from page HEAD. 当您启动刷新时,该页面将不会完全加载到DOM中,因此会从页面HEAD初始化JavaScript。

Read more about this here + you will find several solutions. 在这里阅读有关此内容的更多信息+您将找到几种解决方案。

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

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