本文目錄一覽:
用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
原創文章,作者:DL5VW,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/128355.html