JavaScript的?页面加载后加载一些东西

时间:2017-10-29 19:14:15

标签: javascript php mysql mysqli

我不擅长Javascript,所以我不知道。但是,我想在页面加载后在主体中加载div。

此时页面需要一段时间才能加载,因为它正在加载php,它正在使用json数据对数组中的每个条目对数组进行排序。 (如果这是有道理的)所以这会导致页面的负载增加。

因此,我想首先加载页面,然后将数组列表加载为atm,它只是停留在白色加载屏幕上,直到它全部加载,这使得我的网站看起来很慢。

1 个答案:

答案 0 :(得分:2)

您需要使用AJAX(XMLHttpRequest)和DOM操作(document.getElementById("demo").innerHTML

<!DOCTYPE html>
<html>
<body>

<div id="demo">
<h2>The XMLHttpRequest Object</h2>
<button type="button" onclick="loadDoc()">Change Content</button>
</div>

<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "/your_slow_sort_array.php", true);
  xhttp.send();
}
</script>

</body>
</html>

https://www.w3schools.com/js/js_ajax_http_response.asp

注意:使用Jquery,您可以简化生活

相关问题