所有全局 JavaScript 對象、函數和變量都會自動成為 window 物件的成員。
全局變量是 window 物件的屬性。
全局函數是 window 物件的方法。
甚至(HTML DOM 的)文檔物件也是 window 物件。
if (windowname.closed); if (window.opener.closed);
<body onLoad="window.defaultStatus='Welcome!'";>DefaultStatus.htm
<form> <input type="button" value="Click to search the web" onClick="window.location='http://www.yahoo.com';"> </form>並且這 URL 能夠被即時更改,如
<html> <head> <title>Background Colors</title> <SCRIPT language="JavaScript"> <!-- function gothere() { var thename=document.myform.yourname.value; if (thename=="Lee") window.location="https://www.csie.ntu.edu.tw/~sylee/index.html"; else window.location="http://www.yahoo.com"; } //--> </SCRIPT> </head> <body> <form name="myform"> Your name: <br> <input type="text" name="yourname" size="25"> <p> <input type="button" value="Click for your page" onClick="gothere();"> </form> </body> </html>示範: location.html
也可以將瀏覽器立即轉向到一個新網頁
<html> <head> <title>Background Colors</title> <SCRIPT language="JavaScript"> <!-- window.location="page2.html"; //--> </SCRIPT> </head> <body> Lacking Javascript? Click the link below for the new page then! <br> <a href="page2.html">New Page</a> </body> </html>
<html> <head> <SCRIPT language="JavaScript"> <!-- window.name="cool_window"; //--> </SCRIPT> </head> <body> <SCRIPT language="JavaScript"> <!-- document.write("This window is named " + window.name); //--> </SCRIPT> </body> </html>
<body> <form> <input type="button" value="Click to search the web" onClick="self.location='http://www.yahoo.com';"> </form> </body>
<body> <form> <input type="button" value="Click for Status Message" onClick="window.status='Hello!';"> </form> </body>下例是利用 onMouseOver 事件,用 status 屬性蓋過defaultStatus 屬性的預設值。
<html> <head> <title>Status Bar Sequence</title> </head> <body onLoad="window.defaultStatus='Welcome!';"> <a href="page2.com" onMouseOver="window.status='Page 2'; return true;"> Click for Page 2!</a> </body> </html>
window.alert("Hi there!");
<html> <head> <SCRIPT language="JavaScript"> <!-- function gothere() { var is_sure=window.confirm("Are you sure you want to leave?"); if (is_sure==true) { window.location="http://www.yahoo.com"; } } //--> </SCRIPT> </head> <body> <b>Click the button to see a personalized page!</b> <p> <form> <input type="button" value="Click to search the web" onClick="gothere();"> </form> </body> </html>
例題:confirm.html
<form> <input type="button" value="Click to Find text" onClick=window.find();"> </form>
<form> <input type="button" value="Click to Print Page" onClick=window.print();"> </form>
<html> <head> <SCRIPT language="JavaScript"> <!-- var thename=window.prompt("What's your name?", ""); if ( (thename=="") || (thename==null) ) thename = "Anonymous Visitor"; //--> </SCRIPT> </head> <body> <SCRIPT language="JavaScript"> <!-- document.write("<h1>"); document.write("Hello, " + thename + ", and welcome to my site!"); document.write("</h1>"); //--> </SCRIPT> <p> Other text for the page. </body> </html>
例題:prompt.html
<SCRIPT language="JavaScript"> <!-- window.open("/sylee/courses/jscript/hello.htm", "Hi_window"); //--> </SCRIPT>
例題:pr10_2.html
width height directories location menubar resizable scrollbars status toolbar例題: winattr.html
<html> <head> <SCRIPT language="JavaScript"> <!-- function lauchwin() { window.open("newpage.htm", "cool", "width=400,height=300,status=yes"); } //--> </SCRIPT> </head> <body> Click the button below to open an arrogant new window ... <p> <form> <input type="button" value="New Window" onClick="lauchwin();"> </form> </body> </html>
AlwaysLowered alwaysRaised dependent hotkeys innerHeight innerWidth outerHeight outerWidth personalbar titlebar z-lock screenX screenY left top fullscreenleft, top, 和 fullscreen 三項屬性適用於 Internet Explorer. 假如設定視窗位置的程式要能適應 Netscape 和 Internet Explorer, 在 open() 的參數中就要包括 screenX, screenY, left 和 top 四項屬性。 例如,
<html> <head> <SCRIPT language="JavaScript"> <!-- function launchwin() { window.open("hello.htm", "Hello", "width=400,height=300,status=yes, screenX=0,left=0,screenY=0,top=0"); } //--> </SCRIPT> </head> <body> Click the button below to open a new window ... <p> <form> <input type="button" value="New Window" onClick="launchwin();"> </form> </body> </html>例題: xattr.html
<html> <body> I am a new window! I am newer than that old window that open me, so I am special. Ha, ha! <form> <input type="button" value="Close Window" onClick="window.close();"> </form> </body> </html>
<html> <body> I am a new window! I am newer than that old window that open me, so I am special. Ha, ha! <form> <input type="button" value="Hide Window" onClick="window.blur();"> <input type="button" value="Close Window" onClick="window.close();"> </form> </body> </html>
<html> <body onBlur="window.focus();"> I am a new window! I am newer than that old window that open me, so I am special. Ha, ha! <form> <input type="button" value="Hide Window" onClick="window.blur();"> <input type="button" value="Close Window" onClick="window.close();"> </form> </body> </html>
<html> <body onBlur="window.focus();"> I am a new window! I am newer than that old window that open me, so I am special. Ha, ha! <form> <input type="button" value="Move Window" onClick="window.moveBy(50,50);"> <input type="button" value="Close Window" onClick="window.close();"> </form> </body> </html>
<html> <body onBlur="window.focus();"> I am a new window! I am newer than that old window that open me, so I am special. Ha, ha! <form> <input type="button" value="Move Window" onClick="window.moveTo(50,50);"> <input type="button" value="Close Window" onClick="window.close();"> </form> </body> </html>
<html> <head> <SCRIPT language="JavaScript"> <!-- function annoy_alert() { window.alert("Am I bothering you yet?"); } window.setInterval("annoy_alert()", 10000); //--> </SCRIPT> </head> </html>注意: 引用的函式放在引號內,這是用以阻止函式被立即執行。
<html> <head> <SCRIPT language="JavaScript"> <!-- function annoy_alert() { window.alert("Am I bothering you yet?"); } var madness=window.setInterval("annoy_alert()", 10000); //--> </SCRIPT> </head> <body> Click the button below to end the endless barrage of alerts. <p> <form> <input type="button" value="Stop the Madness" onClick=window.clearInterval(madness);"> </form> </body> </html>
<head> <SCRIPT language="JavaScript"> <!-- function annoy_alert() { window.alert("Sign my guest book NOW!"); } var theguest=window.setTimeout("annoy_alert()", 10000); //--> </SCRIPT> </head>
<html> <head> <SCRIPT language="JavaScript"> <!-- function annoy_alert() { window.alert("Sign my guest book NOW!"); } var theguest=window.setTimeout("annoy_alert()", 10000); //--> </SCRIPT> </head> <body> Click the button below within 10 seconds to avoid an alert message. <p> <form> <input type="button" value="No Alert for Me!" onClick=window.clearTimeout(theguest);"> </form> </body> </html>