检查HTML是否已加载到DIV中

时间:2022-11-10 20:20:03

I want to be able to set up HTML pages and load them into a single home page. Each html file will be named as the date (eg 03052016.html for today) and then the correct html will be pulled in each day on the homepage. However not all days will have a html file, in which case it should roll back to a previous day. I have successfully loaded the page, nice and easy but can't work out a way to identify that the page hasn't loaded and subtract one from the day. My current attempt is the following:

我希望能够设置HTML页面并将它们加载到单个主页中。每个html文件将被命名为日期(例如今天的03052016.html),然后每天在主页上提取正确的html。但是并非所有日子都有html文件,在这种情况下它应该回滚到前一天。我已经成功加载了页面,很简单但很难找到一种方法来识别页面没有加载并从当天减去一个。我目前的尝试如下:

<body>
<div id="success"></div>
  <script>
      //section creates the html file name for today
        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth()+1; //January is 0!
        var yyyy = today.getFullYear();

        if(dd<10) {
            dd='0'+dd
        } 

        if(mm<10) {
            mm='0'+mm
        } 
        today = dd+mm+yyyy+'.html';
        var today = "05052016.html";
      //do loop to subtract days until file is found
        do{
          var found = true; //variable records file presence
          $( "#success" ).load( today, function( response, status, xhr ) {
              if ( status == "error" ) {
                  var found = false;

                  if(parseInt(dd)>1){
                      dd = parseInt(dd)-1;
                  }else {
                      mm = parseInt(mm)-1;
                      dd = 30 //will deal with 31/30/28 day months later.
                  }

                if(dd<10) {
                    dd='0'+dd
                } 

                if(mm<10) {
                    mm='0'+mm
                } 
                today = dd+mm+yyyy+'.html';
                  //
                console.log( msg + xhr.status + " " + xhr.statusText );
              }
            });
        }until(found == false )
    </script>  

I am new to web authoring so please be brutal if I am way off how to implement this. It seems so easy but the loop just won't work! I am currently testing in FireFox, and using jquery-1.10.2.js

我是网络创作的新手,所以如果我离开如何实现这一点,请保持野蛮。这似乎很容易,但循环不起作用!我目前正在FireFox中测试,并使用jquery-1.10.2.js

2 个解决方案

#1


0  

check de length of the content of the quuestioned div.

检查所述div的内容的长度。

        var div = $('div');
        var dHtml = div.html();
        var long = dHtml.length;

       if(long > 0)
       {
          //do stuff
       }

#2


0  

You need to understand that ajax (which is behind load) is asynchronous so you cannot test the found outside the success.

您需要了解ajax(后面的加载)是异步的,因此您无法在成功之外测试找到的内容。

Try this:

function pad(num) {return String("0"+num).slice(-2)}
function getFilename(date)
  var dd = pad(date.getDate());
  var mm = pad(date.getMonth()+1); //January is 0!
  var yyyy = date.getFullYear();
  return ""+dd+mm+yyyy+".html";
}
var date = new Date(), 
    aDay = 10*24*60*60*1000, 
  giveUp = new Date(date.getTime()-(10*aDay)); // max 10 days back
function loadFile(date) {
  $("#success").load( getFilename(date), function() {
    if (status == "error") {
      if (date>giveUp) {
        date.setDate(date.getDate()-1)
        loadFile(date);
      }
    }
  });    
}
$(function() {
  loadFile(date);
});

#1


0  

check de length of the content of the quuestioned div.

检查所述div的内容的长度。

        var div = $('div');
        var dHtml = div.html();
        var long = dHtml.length;

       if(long > 0)
       {
          //do stuff
       }

#2


0  

You need to understand that ajax (which is behind load) is asynchronous so you cannot test the found outside the success.

您需要了解ajax(后面的加载)是异步的,因此您无法在成功之外测试找到的内容。

Try this:

function pad(num) {return String("0"+num).slice(-2)}
function getFilename(date)
  var dd = pad(date.getDate());
  var mm = pad(date.getMonth()+1); //January is 0!
  var yyyy = date.getFullYear();
  return ""+dd+mm+yyyy+".html";
}
var date = new Date(), 
    aDay = 10*24*60*60*1000, 
  giveUp = new Date(date.getTime()-(10*aDay)); // max 10 days back
function loadFile(date) {
  $("#success").load( getFilename(date), function() {
    if (status == "error") {
      if (date>giveUp) {
        date.setDate(date.getDate()-1)
        loadFile(date);
      }
    }
  });    
}
$(function() {
  loadFile(date);
});