试图理解一些与js中readyState相关的代码

时间:2013-07-03 08:51:02

标签: javascript

<head>
    <script type="text/javascript">

        document.onreadystatechange = WaitForComplete;

        function WaitForComplete () {
            console.log ("The state of the document: " + document.readyState);
        }

        function OnLoad () {
            console.log ("The document has been loaded.");
        }
    </script>
</head>
<body onload="OnLoad ()">
</body>

在firefox-&gt;控制台中,它显示:

The state of the document: interactive
The state of the document: complete
The document has been loaded.

问题:

为什么每次我在firefox中运行脚本时,它只显示interactivecomplete?其他州如何:uninitializedloading ...

1 个答案:

答案 0 :(得分:0)

您没有获得状态:uninitialized,loading...,因为您正在使用onreadystatechange函数而不检查上部两个状态。

并且onreadystatechange在文档处于交互状态或完成状态时起作用,并且在加载或未初始化的状态下它将不会调用。

获取加载状态需要添加代码:

document.write(document.readyState);

在脚本开始时不使用onreadystatechange

uninitialized状态你不能执行任何javascript并且无法获得该状态。

相关问题