用 document.readyState == "complete" 判斷頁面是否加載完成。

傳回XML 文件資料的目前狀況。   基本語法 intState = xmlDocument.readyState;   說 明 這個屬性是隻讀的,傳回值有以下的可能: 0-UNINITIALIZED:XML 對象被產生,但沒有任何文件被加載。 1-LOADING:加載程序進行中,但文件尚未開始解析。 2-LOADED:部分的文件已經加載且進行解析,但對象模型尚未生效。 3-INTERACTIVE:僅對已加載的部分文件有效,在此情況下,對象模型是有效但只讀的。 4-COMPLETED:文件已完全加載,代表加載成功。     範 例 alert("The readyState property is " + xmlDoc.readyState);
<script language="javascript"> if (document.readyState=="complete") {         AdjustImageSize(); } else {         document.onreadystatechange = function()         {                             if (document.readyState == "complete")                 {                         AdjustImageSize();                 }         } } function AdjustImageSize() {         var imageWidth = document.all["SendPic"].width;         var imageHeight = document.all["SendPic"].height;                  if (imageWidth == 0 && imageHeight == 0)         {                 document.write ("圖片下載失敗,請刷新!");                 return;         }                  if (imageWidth > 160 || imageHeight > 160)         {                 if (imageWidth > imageHeight)                 {                         k = 160 / imageWidth;                         imageHeight = imageHeight * k;                         imageWidth = 160;                 }                 else                 {                         k = 160 / imageHeight;                         imageWidth = imageWidth * k;                         imageHeight = 160;                 }                                  document.all["ImgResized"].value = "1";         }                  document.all["SendPic"].width = imageWidth;         document.all["SendPic"].height = imageHeight;                  document.all["ImgWidth"].value = imageWidth;         document.all["ImgHeight"].value = imageHeight; } </script> 補充: 如果有這麼一段代碼: If(document.all && document.body.readyState=="complete"){   test(); } 那麼就很有意義了, 上面這段代碼是在頁面加載完成後才執行test()函數. 如果沒有這個判斷,頁面一開始加載到這個語句段就執行這個函數.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章