1. 程式人生 > >js中常用方法以及document.readyState 判斷頁面是否載入完成 complete和interactive

js中常用方法以及document.readyState 判斷頁面是否載入完成 complete和interactive

傳回XML 檔案資料的目前狀況。   基本語法 intState = xmlDocument.readyState;   說 明 這個屬性是隻讀的,傳回值有以下的可能: 0-UNINITIALIZED:XML 物件被產生,但沒有任何檔案被載入。 1-LOADING:載入程式進行中,但檔案尚未開始解析。 2-LOADED:部分的檔案已經載入且進行解析,但物件模型尚未生效。 3-INTERACTIVE:僅對已載入的部分檔案有效,在此情況下,物件模型是有效但只讀的。 4-COMPLETED:檔案已完全載入,代表載入成功。     範 例 alert("The readyState property is " + xmlDoc.readyState);

===========================================

1.視窗關閉時執行的函式 window.onbeforeunload = function(){}

2.頁面載入情況判斷document.readyState值可以是complete和interactive

function document.onreadystatechange()   {   if(document.readyState=="complete")     alert(document.readyState); }

或者:

document.onreadystatechange = init;
function init() {

if(document.readyState=="complete")     {.........}

}

3.遮蔽右鍵功能和嚴禁選中操作

//document.oncontextmenu=new Function("event.returnValue=false;");

 //document.onselectstart=new Function("event.returnValue=false;");

4.滑鼠位置判斷

window.event.y和window.event.x   //x,y是滑鼠相對於當前瀏覽器的位置

window.event.screenY和window.event.screenX   //screenX,screenY是相對於使用者顯示器

的位置

window.event.clientY和window.event.clientX       //clientX, clientY是滑鼠當前相對於網頁的位置,

                                                                             //當滑鼠位於頁面左上角時clientX=0, clientY=0;為負數是表示不在網頁內;

window.event.offsetY和window.event.offsetX       //offsetX, offsetY是滑鼠當前相對於網頁中的某一區域的位置,當滑鼠位於頁面中這一區域的左上角時offsetX=0, offsetY=0;

5.視窗大小判斷

document.documentElement.scrollWidth和document.documentElement.scrollHeight //獲取視窗的寬和高

6.返回值

window.event.returnValue="真的要關閉嗎";       //彈出一個確認資訊,確認事件是否要執行

return confirm("真的要關閉嗎");                       //兩個是一樣的功能

7.獲取隨機數 parseInt(Math.random()*100)   //獲取1至100之間的隨機數