document.readyState

2018-03-08 09:53:39
(.readyState === ) {
   .();
}
{
   .(, .);
}

概述


一个document 的 Document.readyState 属性描述了文档的加载状态。

一个文档的 readyState 可以是以下之一:

  • loading / 加载

  • document 仍在加载。

  • interactive / 互动

  • 文档已经完成加载,文档已被解析,但是诸如图像,样式表和框架之类的子资源仍在加载。

  • complete / 完成

  • T文档和所有子资源已完成加载。状态表示 load 事件即将被触发。

当这个属性的值变化时,document 对象上的readystatechange 事件将被触发。

 

语法

let string = document.readyState;// "complete"

例子

不同的准备状态

switch (document.readyState) {
  case "loading":
    // The document is still loading.
    break;
  case "interactive":
    // The document has finished loading.
    // We can now access the DOM elements.
    var span = document.createElement("span");
    span.textContent = "A <span> element.";
    document.body.appendChild(span);
    break;
  case "complete":
    // The page is fully loaded.
    let CSS_rule = document.styleSheets[0].cssRules[0].cssText;
    console.log(`The first CSS rule is: ${CSS_rule }`);
    break;}
// 模拟 DOMContentLoaded/ jquery readydocument.onreadystatechange = function () {
  if (document.readyState === "interactive") {
    initApplication();
  }}
// 模拟 load/onload 事件document.onreadystatechange = function () {
  if (document.readyState === "complete") {
    initApplication();
  }}

相关链接

规范

  • 2018-09-26 15:14:23

    PHP JSON_ENCODE 不转义中文汉字的方法

    PHP 生成JSON的时候,必须将汉字不转义为 \u开头的UNICODE数据。 网上很多,但是其实都是错误的,正确的方法是在json_encode 中加入一个参数 JSON_UNESCAPED_UNICODE

  • 2018-09-27 10:04:11

    jquery ajax超时设置

    原来ajax可以设置超时时间,那么简单,ajax还有更多功能,虽然不怎么用它,有时候还挺好用。

  • 2018-10-10 14:29:01

    php header()函数设置页面Cache缓存

    ​ header()函数在php的使用很大,下面我来介绍利用它实现页面缓存的一些方法,但使用header前必须注意,在它之前不能任何输出,包括空格。