本文目录一览:
用js判断页面是否加载完成
用document.onreadystatechange的方法来监听状态改变, 然后用document.readyState == “complete”判断是否加载完成 代码如下: document.onreadystatechange = subSomething;//当页面加载状态改变的时候执行这个方法. function subSomething() { if(document.readyState == “complete”) //当页面加载状态 myform.submit(); //表单提交 } 页面加载readyState的五种状态 原文如下: 0: (Uninitialized) the send( ) method has not yet been invoked. 1: (Loading) the send( ) method has been invoked, request in progress. 2: (Loaded) the send( ) method has completed, entire response received. 3: (Interactive) the response is being parsed. 4: (Completed) the response has been parsed, is ready for harvesting. 翻译成中文为: 0 - (未初始化)还没有调用send()方法 1 - (载入)已调用send()方法,正在发送请求 2 - (载入完成)send()方法执行完成,已经接收到全部响应内容 3 - (交互)正在解析响应内容 4 - (完成)响应内容解析完成,可以在客户端调用了
用js判断页面是否加载完成实现代码
然后用document.readyState == “complete”判断是否加载完成代码如下:代码如下: document.onreadystatechange = subSomething;//当页面加载状态改变的时候执行这个方法. function subSomething() { if(document.readyState == “complete”) //当页面加载状态 myform.submit(); //表单提交 } 页面加载readyState的五种状态原文如下:0: (Uninitialized) the send( ) method has not yet been invoked. 1: (Loading) the send( ) method has been invoked, request in progress. 2: (Loaded) the send( ) method has completed, entire response received. 3: (Interactive) the response is being parsed. 4: (Completed) the response has been parsed, is ready for harvesting. 翻译成中文为: 0 - (未初始化)还没有调用send()方法 1 - (载入)已调用send()方法,正在发送请求 2 - (载入完成)send()方法执行完成,已经接收到全部响应内容 3 - (交互)正在解析响应内容4 - (完成)响应内容解析完成,可以在客户端调用了
如何让页面加载完成后执行js
让页面加载完执行js有2种方法,js放在文档代码的下方和把语句代码放在window.onload方法里面。
js放在文档代码的下方
这是一个最简单的文档结构,引用的javascript文件都放在body的最下方和把语句放在window.onload函数里面,可以让javascript在页面加载完成后执行。
!doctype html
html
head
titlehelloWorld--zxk/title
/head
body
div/div
script src="xxx"/script
/body
/html
把语句代码放在window.onload函数里面
例如这段代码,虽然script标签没有在最下方,但javascript语句放在了window.onload里面,所以可以在文档加载完成后执行。
!doctype html
html
head
titlehelloWorld--zxk/title
script
window.onload=function(){
alert('页面加载完成!');
}
/script
/head
body
div/div
/body
/html