window對象

Window對象的屬性

1.closed

返回一個布爾值,該值聲明瞭窗口是否已經關閉

History 對象是 window 對象的一部分,可通過 window.history 屬性對其進行訪問。

註釋:沒有應用於 History 對象的公開標準,不過所有瀏覽器都支持該對象。

返回值

返回一個布爾值,true爲關閉,false爲未關閉

瀏覽器支持

google IE firefox safari opera
true true true true true

window.closed

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>zsh</title>
<script>
var myWindow;
function openWin(){
    myWindow=window.open("","","width=400,height=200");
}
function closeWin(){
    if (myWindow){
        myWindow.close();
    }
}
function checkWin(){
    if (!myWindow){
        document.getElementById("msg").innerHTML="我的窗口沒有被打開!";
    }
    else{
        if (myWindow.closed){
            document.getElementById("msg").innerHTML="我的窗口被關閉!";
        }
        else{
            document.getElementById("msg").innerHTML="我的窗口沒有被關閉!";
        }
    }   
}
</script>
</head>
<body>

<input type="button" value="打開我的窗口" onclick="openWin()" />
<input type="button" value="關閉我的窗口" onclick="closeWin()" />
<br><br>
<input type="button" value="我的窗口被關閉了嗎?" onclick="checkWin()" />
<br><br>
<div id="msg"></div>

</body>
</html>

2.frames

性返回窗口中所有命名的框架

該集合是 Window 對象的數組,每個 Window 對象在窗口中含有一個框架或 <iframe>。屬性 frames.length 存放數組 frames[] 中含有的元素個數。

注意,frames[] 數組中引用的框架可能還包括框架,它們自己也具有 frames[] 數組。

提示: 使用 frames.length 來獲取框架的數量。

返回值

返回一個布爾值,true爲關閉,false爲未關閉

瀏覽器支持

google IE firefox safari opera
true true true true true

window.closed

<html>
<body>

<iframe src="http://microsoft.com"></iframe>
<iframe src="http://google.com"></iframe>
<iframe src="http://youtube.com"></iframe>

<script>
for (var i=0; i<frames.length; i++)
  {
  frames[i].location="http://w3cschool.cc"
  }
</script>

</body>
</html>

3. innerHeight

返回窗口的文檔顯示區的高度

注意:使用 outerWidth 和 outerHeight 屬性獲取加上工具條與滾動條窗口的寬度與高度。

瀏覽器支持

google IE firefox safari opera
true 9.0 true true true

window.innerHeight | innerHeight

console.log(window.innerHeight);

4. innerWidth

返回窗口的文檔顯示區的寬度

注意:使用 outerWidth 和 outerHeight 屬性獲取加上工具條與滾動條窗口的寬度與高度。

瀏覽器支持

google IE firefox safari opera
true 9.0 true true true

window.innerWidth| innerWidth

console.log(window.innerWidth);

5. length

返回在當前窗口中frames的數量(包括IFRAMES)

瀏覽器支持

google IE firefox safari opera
true true true true true

window.length| length

<html>
<body>

<iframe src="http://www.microsoft.com"></iframe>
<iframe src="http://www.google.com"></iframe>
<iframe src="http://www.youtube.com"></iframe>

<script>
for (var i=0; i<frames.length; i++)
{
    frames[i].location="http://www.runoob.com"
}
</script>

</body>
</html>

6. name

設置或返回存放窗口的名稱的一個字符串

瀏覽器支持

google IE firefox safari opera
true true true true true

window.name

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>zsh</title>
<script>
var myWindow;
function openWin(){
    myWindow=window.open('http://www.zshgrwz.cn','MsgWindow');
    myWindow.document.write("<p>窗口名稱爲: " + myWindow.name + "</p>");
}
</script>
</head>
<body>

<input type="button" value="打開我的窗口" onclick="openWin()" />

</body>
</html>

7. opener

可讀可寫的屬性,可返回對創建該窗口的 Window 對象的引用

當使用window.open()打開一個窗口,您可以使用此屬性返回來自目標窗口源(父)窗口的詳細信息。

提示: window.opener.close()將關閉源(父)窗口。

瀏覽器支持

google IE firefox safari opera
true true true true true

window.opener

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>zsh</title> 
<script> 
function openWin(){ 
    myWindow=window.open('','','width=200,height=100'); 
    myWindow.document.write("<p>這是我的窗口</p>"); 
    myWindow.focus(); 
    myWindow.opener.document.write("<p>這個是源窗口!</p>"); 
} 
</script> 
</head> 
<body> 

<input type="button" value="打開我的窗口" onclick="openWin()" /> 

</body> 
</html>

8. outerWidth

設置或返回窗口的外部寬度,包括所有的界面元素(如工具欄/滾動)

注意: 使用 innerWidth 和 innerHeight 屬性獲取去除工具條與滾動條的窗口高度與寬度。

瀏覽器支持

google IE firefox safari opera
true 9.0 true true true

window.outerWidth

console.log(window.outerWidth);

9. outerHeight

設置或返回窗口的外部高度,包括所有的界面元素(如工具欄/滾動)

注意: 使用 innerWidth 和 innerHeight 屬性獲取去除工具條與滾動條的窗口高度與寬度。

瀏覽器支持

google IE firefox safari opera
true 9.0 true true true

window.outerHeight

console.log(window.outerHeight );

10. pageXOffset

返回當前頁面相對於窗口顯示區左上角的 X 位置

注意: 使用 innerWidth 和 innerHeight 屬性獲取去除工具條與滾動條的窗口高度與寬度。

瀏覽器支持

google IE firefox safari opera
true 9.0 true true true

window.pageXOffset

console.log(window.pageXOffset);
//兼容方案 ie8 console.log(document.documentElement.scrollLeft );

11. pageYOffset

返回當前頁面相對於窗口顯示區左上角的 X 位置

注意: 使用 innerWidth 和 innerHeight 屬性獲取去除工具條與滾動條的窗口高度與寬度。

瀏覽器支持

google IE firefox safari opera
true 9.0 true true true

window.pageYOffset

console.log(window.pageYOffset );
//兼容方案 ie8 console.log(document.documentElement.scrollTop );

12. parent

返回當前窗口的父窗口

瀏覽器支持

google IE firefox safari opera
true true true true true

window.parent

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>zsh</title>
</head>
<head>
<script>
function openWin(){
    window.open('','','width=200,height=100');
    alert(window.parent.location);
}
</script>
</head>
<body>

<input type="button" value="打開窗口" onclick="openWin()">

</body>
</html>

13. screenLeft

返回窗口相對於屏幕的X座標

瀏覽器支持

google IE firefox safari opera
true true false true true

注意: Firefox 瀏覽器請使用 “window.screenX” and “window.screenY”。

window.screenLeft

console.log(window.screenLeft);

14. screenTop

返回窗口相對於屏幕的Y座標

瀏覽器支持

google IE firefox safari opera
true true false true true

注意: Firefox 瀏覽器請使用 “window.screenX” and “window.screenY”。

window.screenTop

console.log(window.screenTop);

15. screenX

返回窗口相對於屏幕的X座標

瀏覽器支持

google IE firefox safari opera
true false true true true

注意: IE瀏覽器使用。 “window.screenLeft” 和 “window.screenTop”獲得相同的值

window.screenX

console.log(window.screenX );

16. screenY

返回窗口相對於屏幕的Y座標

瀏覽器支持

google IE firefox safari opera
true false true true true

注意: IE瀏覽器使用。 “window.screenLeft” 和 “window.screenTop”獲得相同的值

window.screenY

console.log(window.screenY);

17. self

返回對窗口自身的只讀引用

瀏覽器支持

google IE firefox safari opera
true true true true true

window.self

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>zsh</title> 
<script> 
function check(){ 
    if (window.top!=window.self) { 
        document.write("<p>這個窗口不是最頂層窗口!我在一個框架?</p>") 
    } 
    else{ 
        document.write("<p>這個窗口是最頂層窗口!</p>") 
    } 
} 
</script> 
</head> 
<body> 

<input type="button" onclick="check()" value="檢查窗口"> 

</body> 
</html>

18. top

返回當前窗口的最頂層瀏覽器窗口

瀏覽器支持

google IE firefox safari opera
true true true true true

window.top

console.log(window.top);

Window 對象方法

1. alert

用於顯示帶有一條指定消息和一個 確認 按鈕的警告框

瀏覽器支持

google IE firefox safari opera
true true true true true

alert(message);

參數

  • 必需
    • message 字符串,如果不是字符串會被強制轉化爲字符串
alert("你好,我是一個警告框!");

2. blur

可把鍵盤焦點從頂層窗口移開

瀏覽器支持

google IE firefox safari opera
true true true true false

window.blur()

<!DOCTYPE html>
<html>
<head>
<script>
function openWin()
{
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("<p>The new window.</p>");
myWindow.blur();
}
</script>
</head>
<body>

<input type="button" value="Open window" onclick="openWin()">

</body>
</html>

3. clearInterval

可取消由 setInterval() 設置的 timeout。

clearInterval() 方法的參數必須是由 setInterval() 返回的 ID 值。

瀏覽器支持

google IE firefox safari opera
true true true true true

clearInterval(id_of_setinterval)

參數

  • 必需
    • id_of_setinterval 由 setInterval() 返回的 ID 值。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>zsh</title>
</head>
<body>

<input type="text" id="clock" />
<script type="text/javascript">
var int=self.setInterval("clock()",1000);
function clock(){
    var d=new Date();
    var t=d.toLocaleTimeString();
    document.getElementById("clock").value=t;
}
</script>
<button onclick="int=window.clearInterval(int)">停止</button>

</body>
</html>

4. clearTimeout

可取消由 setTimeout() 方法設置的 timeout

clearTimeout()方法的參數必須是由setTimeout()返回的ID值。

瀏覽器支持

google IE firefox safari opera
true true true true true

clearTimeout(id_of_settimeout)

參數

  • 必需
    • id_of_settimeout 由 setTimeout() 返回的 ID 值。
<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>zsh</title> 
<script>
var c=0;
var t;
var timer_is_on=0;
function timedCount(){
    document.getElementById('txt').value=c;
    c=c+1;
    t=setTimeout(function(){timedCount()},1000);
}
function doTimer(){
    if (!timer_is_on){
        timer_is_on=1;
        timedCount();
    }
}
function stopCount(){
    clearTimeout(t);
    timer_is_on=0;
}
</script>
</head>
<body>

<form>
<input type="button" value="開始計數!" onclick="doTimer()" />
<input type="text" id="txt" />
<input type="button" value="停止計數!" onclick="stopCount()" />
</form>
<p>
單擊開始計數按鈕,按下時開始計數,輸入框將從0開始一直計數。單擊停止計數按鈕,按下時停止計數,再次點擊開始計數按鈕,又再次開始計數。
</p>
</body>

</html>

5. close

用於關閉瀏覽器窗口

瀏覽器支持

google IE firefox safari opera
true true true true true

window.close()


window.close();

6. confirm

用於顯示一個帶有指定消息和確認及取消按鈕的對話框

提示:如果訪問者點擊”確定”,此方法返回true,否則返回false。

瀏覽器支持

google IE firefox safari opera
true true true true true

confirm(message)

參數

  • 必需
    • message 字符串,如果不是字符串會被強制轉化爲字符串
var x; 
    var r=confirm("按下按鈕!"); 
    if (r==true){ 
        x="你按下了\"確定\"按鈕!"; 
    } 
    else{ 
        x="你按下了\"取消\"按鈕!"; 
    } 
    document.getElementById("demo").innerHTML=x; 

7. focus

可把鍵盤焦點給予一個窗口

瀏覽器支持

google IE firefox safari opera
true true true true true

window.focus()

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>zsh</title>
<script>
function openWin(){
    myWindow=window.open('','','width=200,height=100');
    myWindow.document.write("<p>這是'我的窗口'</p>");
    myWindow.focus();
}
</script>
</head>
<body>

<input type="button" value="打開窗口" onclick="openWin()" />

</body>
</html>

8. moveBy

可相對窗口的當前座標把它移動指定的像素

瀏覽器支持

google IE firefox safari opera
true true true true true

window.moveBy(x,y)

參數

  • 必需
    • x 相對於左邊的距離
    • y 相對於頂部的距離
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>zsh</title>
    <script>
        function openWin(){
            myWindow=window.open('','','width=200,height=100');
            myWindow.document.write("<p>這是我的窗口</p>");
        }
        function moveWin(){
            myWindow.moveBy(250,250);
            myWindow.focus();
        }
    </script>
</head>
<body>

<input type="button" value="打開我的窗口" onclick="openWin()" />
<br><br>
<input type="button" value="移動我的窗口" onclick="moveWin()" />

</body>
</html>

9. moveTo

可把窗口的左上角移動到一個指定的座標

瀏覽器支持

google IE firefox safari opera
true true true true true

注意: 指定moveTo(0.0),在Opera瀏覽器的左上角。

window.moveTo(x,y)

參數

  • 必需
    • x 相對於左邊的距離
    • y 相對於頂部的距離
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>zsh</title>
<script>
function openWin(){
    myWindow=window.open('','','width=200,height=100');
    myWindow.document.write("<p>這是我的窗口</p>");
}
function moveWin(){
    myWindow.moveTo(0,0);
    myWindow.focus();
}
</script>
</head>
<body>

<input type="button" value="打開窗口" onclick="openWin()" />
<br><br>
<input type="button" value="移動窗口" onclick="moveWin()" />

</body>
</html>

10. open

用於打開一個新的瀏覽器窗口或查找一個已命名的窗口

瀏覽器支持

google IE firefox safari opera
true true true true true

window.open(URL,name,specs,replace)

參數

  • 可選
    • URL 打開指定的頁面的URL。如果沒有指定URL,打開與新的空白窗口。
    • name 指定target屬性或窗口的名稱。支持以下值:
      _blank - URL加載到一個新的窗口。這是默認
      _parent - URL加載到父框架
      _self - URL替換當前頁面
      _top - URL替換任何可加載的框架集
      name - 窗口名稱
    • specs 一個逗號分隔的項目列表。支持以下值:
      • channelmode=yes|no|1|0 是否要在影院模式顯示 window。默認是沒有的。僅限IE瀏覽器
      • directories=yes|no|1|0 是否添加目錄按鈕。默認是肯定的。僅限IE瀏覽器
      • fullscreen=yes|no|1|0 瀏覽器是否顯示全屏模式。默認是沒有的。在全屏模式下的 window,還必須在影院模式。僅限IE瀏覽器
      • height=pixels 窗口的高度。最小.值爲100
      • left=pixels 該窗口的左側位置
      • location=yes|no|1|0 是否顯示地址字段.默認值是yes
      • menubar=yes|no|1|0 是否顯示菜單欄.默認值是yes
      • resizable=yes|no|1|0 是否可調整窗口大小.默認值是yes
      • scrollbars=yes|no|1|0 是否顯示滾動條.默認值是yes
      • status=yes|no|1|0 是否要添加一個狀態欄.默認值是yes
      • titlebar=yes|no|1|0 是否顯示標題欄.被忽略,除非調用HTML應用程序或一個值得信賴的對話框.默認值是yes
      • toolbar=yes|no|1|0 是否顯示瀏覽器工具欄.默認值是yes
      • top=pixels 窗口頂部的位置.僅限IE瀏覽器
      • width=pixels 窗口的寬度.最小.值爲100
    • replace Optional.Specifies規定了裝載到窗口的 URL 是在窗口的瀏覽歷史中創建一個新條目,還是替換瀏覽歷史中的當前條目。支持下面的值:
      • true - URL 替換瀏覽歷史中的當前條目。
      • false - URL 在瀏覽歷史中創建新的條目。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>zsh</title>
<script>
function open_win() {
    window.open("http://www.zshgrwz.cn");
}
</script>
</head>
<body>

<form>
<input type="button" value="打開窗口" onclick="open_win()">
</form>

</body>
</html>

11. print

用於打印當前窗口的內容

瀏覽器支持

google IE firefox safari opera
true true true true true

window.print()

window.print();

12. prompt

用於顯示可提示用戶進行輸入的對話框

返回值

這個方法返回用戶輸入的字符串。

瀏覽器支持

google IE firefox safari opera
true true true true true

prompt(msg,defaultText)

參數

  • 可選
    • msg 要在對話框中顯示的純文本(而不是 HTML 格式的文本)。
    • defaultText 默認的輸入文本。
prompt("你確定要點確定嗎?","不許你點");

13. resizeTo

用於把窗口大小調整爲指定的寬度和高度

瀏覽器支持

google IE firefox safari opera
true true true true true

window.resizeTo(width,height)

參數

  • 必需
    • width 設置窗口的寬度,以像素爲單位。
    • height 設置窗口的高度,以像素爲單位。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>zsh</title>
<script>
var w;
function openwindow(){
    w=window.open('','', 'width=100,height=100');
    w.focus();
}
function myFunction(){
    w.resizeTo(500,500);
    w.focus();
}
</script>
</head>
<body>

<button onclick="openwindow()">創建窗口</button>
<button onclick="myFunction()">調整窗口</button>

</body>
</html>

14. scrollBy

可把內容滾動指定的像素數

注意: 要使此方法工作 window 滾動條的可見屬性必須設置爲true!

瀏覽器支持

google IE firefox safari opera
true true true true true

window.scrollBy(xnum,ynum)

參數

  • 必需
    • xnum 把文檔向右滾動的像素數。
    • ynum 把文檔向下滾動的像素數。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>zsh</title>
<script>
function scrollWindow(){
    window.scrollBy(100,100);
}
</script>
</head>
<body>

<input type="button" onclick="scrollWindow()" value="滾動條" />
<p>滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 </p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 </p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 </p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 </p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 </p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 </p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 </p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 </p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>

</body>
</html>

15. scrollTo

可把內容滾動指定的座標

注意: 要使此方法工作 window 滾動條的可見屬性必須設置爲true!

瀏覽器支持

google IE firefox safari opera
true true true true true

window.scrollBy(xpos,ypos)

參數

  • 必需
    • xpos 要在窗口文檔顯示區左上角顯示的文檔的 x 座標。
    • 要在窗口文檔顯示區左上角顯示的文檔的 y 座標。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>zsh</title>
<script>
function scrollWindow(){
    window.scrollTo(100,100);
}
</script>
</head>
<body>

<input type="button" onclick="scrollWindow()" value="滾動條" />
<p>滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 </p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 </p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 </p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 </p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 </p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 </p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 </p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 滾動 </p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>

</body>
</html>

16. setInterval

可按照指定的週期(以毫秒計)來調用函數或計算表達式

setInterval() 方法會不停地調用函數,直到 clearInterval() 被調用或窗口被關閉。由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的參數。

提示: 1000 毫秒= 1 秒。

google IE firefox safari opera
true true true true true

setInterval(code,millisec,lang)

參數

  • 必需
    • code 要調用的函數或要執行的代碼串。
    • millisec 週期性執行或調用 code 之間的時間間隔,以毫秒計。
  • 可選
    • lang JScript | VBScript | JavaScript。
<html>
<body>

<input type="text" id="clock">
<script language=javascript>
var int=self.setInterval(function(){clock()},1000);
function clock()
  {
  var d=new Date();
  var t=d.toLocaleTimeString();
  document.getElementById("clock").value=t;
  }
</script>

<button onclick="int=window.clearInterval(int)">Stop</button>

</body>
</html>

17. setTimeout

用於在指定的毫秒數後調用函數或計算表達式

提示: 1000 毫秒= 1 秒。

google IE firefox safari opera
true true true true true

setTimeout(code,millisec,lang)

參數

  • 必需
    • code 要調用的函數後要執行的 JavaScript 代碼串。
    • millisec 在執行代碼前需等待的毫秒數。
  • 可選
    • lang JScript | VBScript | JavaScript。
var t=setTimeout(function(){alert("Hello")},3000)

文檔內容出自 W3cSchool和菜鳥教程,
如需查看更詳細的有關內容 請登錄 http://www.w3school.com.cn/http://www.runoob.com/

發佈了44 篇原創文章 · 獲贊 12 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章