JavaScript Tutorial/Window/Window Object

Материал из Web эксперт
Перейти к: навигация, поиск

Window

The Window object is a JavaScript object created when a <body>, <frameset>, or <frame> tag is encountered.

Instances of this object can also be created by using the Window.open() method.

Methods/Properties Description alert Loads an alert dialog box with the text string passed. back Loads the previous page in place of the window instance. blur Removes the focus from a window. captureEvents Sets the window to capture all events of a specified type. clearInterval Clears the interval set with the setInterval method. clearTimeout Clears the timeout set with the setTimeout method. close Closes the instance of the window. confirm Displays a confirmation dialog box. disableExternalCapture Disables external event capturing. enableExternalCapture Enables external event capturing for the pages loaded from other servers. find Displays a Find dialog box where the user can enter text to search the _current page. focus Assigns the focus to the specified _window instance. forward Loads the next page in place of the window instance. handleEvent Invokes the handler for the event passed. home Loads the user"s specified home page in place of the window instance. moveBy Moves the window by the amounts specified. moveTo Moves the window to the specified location. open Opens a new instance of a window. print Invokes the Print dialog box so the user can print the current window. prompt Displays a prompt dialog box. releaseEvents Releases the captured events of a specified type. resizeBy Resizes the window by the specified amount. resizeTo Resizes the window to the specified size. routeEvent Passes the events of a specified type to be handled natively. scroll Scrolls the document in the window to a specified location. scrollBy Scrolls the document in the window by a specified amount. scrollTo Scrolls the document, both width and height, in the window to a specified location. setInterval Invokes a function or evaluates an expression every time the number of milliseconds has passed. setTimeout Invokes a function or evaluates an expression when the number of milliseconds has passed. stop Stops the current window from loading other items within it. closed Specifies if the window instance has been closed. defaultStatus Is the default message in the window"s status bar. document References all the information about the document within this window. See the Document object for more information. frames References all the information about the frames within this window. See the Frame object for more information. history References the URLs the user has _visited. innerHeight Contains the height of the display area of the current window in pixels. innerWidth Contains the width of the display area of the current window in pixels. length Represents the number of frames in the current window. location Contains the current URL loaded into the window. locationbar Reference to the browser"s location bar. menubar Reference to the browser"s menu bar. name Name of the window. opener Contains the name of the window from which a second window was opened. outerHeight Contains the height of the outer area of the current window in pixels. outerWidth Contains the width of the outer area of the current window in pixels. pageXOffset Contains the x-coordinate of the current window. pageYOffset Contains the y-coordinate of the current window. parent Reference to the upper most window that is displaying the current frame. personalbar Reference to the browser"s personal bar. scrollbars Reference to the browser"s scroll bars. self Reference for the current window. status Reference to the message in the window"s status bar. statusbar Reference to the browser"s status bar. toolbar Reference to the browser"s tool bar. top Reference to the upper most window that is displaying the current frame. window Reference for the current window.

When using the close() and open() methods, you must take the instance"s name when called within an event handler.



   <source lang="javascript">

<html>

   <head>
   <script language="JavaScript">
   
   </script>
   </head>
   <body>
   <form>
     Click the following button to open a new window: 
     <input type=BUTTON value="Open" onClick="openWin()">
   </form>
   </body>
   </html></source>
   
  

window.alert()

Syntax



   <source lang="javascript">

window.alert(string)</source>


window.back()

The back() method simulates the user clicking the Back button on the browser.

It returns the browser"s page or frame to the previous page or frame.



   <source lang="javascript">

<html>

   <body>
   <form>
     <input type=BUTTON value="Back" onClick="window.back()">
     <input type=BUTTON value="Forward" onClick="window.forward()">
   </form>
   </body>
   </html></source>
   
  

window.captureEvents()

Syntax



   <source lang="javascript">

window.captureEvents(event)

   window.captureEvents(event1 | event2 | eventN)</source>
   
  

window.clearInterval()

Syntax



   <source lang="javascript">

window.clearInterval(interval)</source>


window.clearTimeout()

Syntax



   <source lang="javascript">

window.clearTimeout(timeout)</source>


window.defaultStatus

Syntax



   <source lang="javascript">

window.defaultStatus = string</source>


window.disableExternalCapture()

Syntax



   <source lang="javascript">

window.disableExternalCapture()</source>


window.document

Syntax



   <source lang="javascript">

window.document.event

   window.document.method()
   window.document.property</source>
   
  

The document property is a child object of the Window object created when instances of the <body> tag are encountered.

Event Handlers/Methods/Properties Description onClick Called when the document is clicked. onDblClick Called when the document is double-clicked. onKeyDown Called when a key is pressed down. This occurs before an onKeyPress event handler. onKeyPress Called when a key is pressed down immediately after an onKeyDown event handler. onKeyUp Called when a key is released. onMouseDown Called when the mouse button is pressed down. onMouseUp Called when the mouse button is released. captureEvents() Allows you to capture all events of the type passed in the document. close() Closes the stream to the document. getSelection() Returns the currently selected text. handleEvent() Invokes the handler for the event specified. open() Opens a stream to the document. releaseEvents() Releases the events that you have captured of the type passed in the document. routeEvent() Passes the specified event along the normal route of execution. write() Writes the string passed to the document. writeln() Writes the string, followed by a newline character, to the document. alinkColor Specifies the ALINK attribute of the <body> tag. anchors Array containing each <a> tag in a document. applets Array containing each <applet> tag in a document. bgColor Specifies the BGCOLOR attribute of the <body> tag. cookie Specifies a cookie. domain Specifies the domain that served the document. embeds Array containing each <embed> tag in a document. fgColor Specifies the TEXT attribute of the <body> tag. formName The actual name of each <form> in a document. forms Array containing each <form> tag in a document. images Array containing each tag in a document. lastModified Specifies the date the document was last changed. layers Array containing each <layer> tag in a document. linkColor Specifies the LINK attribute of the <body> tag. links Array containing each <a> and <area> tag in a document. plugins Array containing each plug-in in a document. referrer Specifies the referral URL. title Contains the text between the beginning and ending </title> tags. URL Specifies the URL of the document. vlinkColor Specifies the VLINK attribute of the <body> tag.

window.enableExternalCapture()

Syntax



   <source lang="javascript">

window.enableExternalCapture(event)</source>


window.find()

The find() method displays a find dialog box when invoked.

This allows a user to search for a string in the page from which it was invoked.



   <source lang="javascript">

<html>

   <script language="JavaScript1.2">
   
   </script>

</html></source>


window.focus()

The focus() method places focus on the window.



   <source lang="javascript">

<html>

   <script language="JavaScript">
   
   </script>
   <body>
   <form>
     <input type=BUTTON value="Open" onClick="openWin()">
   </form>
   </body>
   </html></source>
   
  

window.forward()

The forward() method simulates the user clicking the Forward button on the browser.

It returns the browser"s page or frame to the next page or frame in its history.



   <source lang="javascript">

<html>

   <body>
   <form>
     <input type=BUTTON value="Back" onClick="window.back()">
     <input type=BUTTON value="Forward" onClick="window.forward()">
   </form>
   </body>
   </html></source>
   
  

window.frames

Syntax



   <source lang="javascript">

window.frames["frameName"]

   window.frames[num]</source>
   
  

window.frames.length

Syntax



   <source lang="javascript">

window.frames["frameName"].length

   window.frames[num].length</source>
   
  

window.home()

The home() method simulates the user clicking the Home button on the browser.

It takes the browser to the user"s specified home page.



   <source lang="javascript">

<html>

   <body>
   <form>

Home James!

     <input type=BUTTON value="Home" onClick="window.home()">
   </form>
   </body>
   </html></source>
   
  

window.innerHeight

The innerHeight property references the pixel height of the document within the browser"s frame.

This does not include any of the toolbars that makes up the frame itself.



   <source lang="javascript">

<html>

   <head>
   <script language="JavaScript1.2">
   
   </script>
   </head>
   <body>
   <form>
     <input type=BUTTON value="Open" onClick="openWin()">
   </form>
   </body>
   </html></source>
   
  

window.innerWidth

The innerWidth property references the pixel width of the document within the browser"s frame.

This does not include any of the toolbars that makes up the frame itself.



   <source lang="javascript">

<html>

   <head>
   <script language="JavaScript1.2">
   
   </script>
   </head>
   <body>
   <form>
     <input type=BUTTON value="Open" onClick="openWin()">
   </form>
   </body>
   </html></source>
   
  

window.length

The length property represents the number of frames within a window.

This returns the same results as Window.frames.length.



   <source lang="javascript">

<html>

   <script language="JavaScript">
   
   </script>

</html></source>


window.locationbar

The real use of this property is to access its visible property.



   <source lang="javascript">

window.locationbar.visible</source>


window.menubar

The real use of this property is to access its visible property.



   <source lang="javascript">

window.menubar.visible</source>


window.moveBy()

Syntax



   <source lang="javascript">

window.moveBy(numHort, numVert)</source>


window.moveTo()

Syntax



   <source lang="javascript">

window.moveTo(numX, numY)</source>


window.name

The name property returns the name of the window. This property contains the name specified when new windows are created using the Window.open() method.



   <source lang="javascript">

<html>

   <head>
   <script language="JavaScript">
   
   </script>
   </head>
   <body>
   <form>
     <input type=BUTTON value="Open" onClick="openWin()">
   </form>
   </body>
   </html></source>
   
  

window.onBlur

The onBlur event handler is fired when the focus is moved away from that particular window instance.



   <source lang="javascript">

<html>

   <frameset cols="150,*">
     <frame name="toc"
            src="/toc.htm"
            onBlur="myBlurFunc()"
            marginwidth=1 marginheight=1 scrolling=AUTO>
     <frame name="body"
            src="/body.htm"
            marginwidth=10 marginheight=5 scrolling=AUTO>
   </frameset>

</html></source>


window.onDragDrop

The onDragDrop event handler is fired when the user drops an object.



   <source lang="javascript">

<html>

   <body onDragDrop="return(confirm("Are you sure you want to continue?"))">
   </body>
   Try to drop an element on this page.
   </html></source>
   
  

window.onError

The onError event handler is fired when an error occurs loading the page.



   <source lang="javascript">

<html>

   <body onError="alert("Error: There has been an error loading this page.")">

</html></source>


window.onFocus

The onFocus event handler is fired when the focus is placed on that particular window instance.



   <source lang="javascript">

<html>

   <frameset cols="150,*">
     <frame name="toc"
            src="/toc.htm"
            onFocus="myFocusFunc()"
            marginwidth=1 marginheight=1 scrolling=AUTO>
     <frame name="body"
            src="/body.htm"
            marginwidth=10 marginheight=5 scrolling=AUTO>
   </frameset>

</html></source>


window.onLoad

The onLoad event handler is fired when the page has finished loading.

The onLoad event handler in the <body> of a document fires before the event handler in the <frameset> tag.



   <source lang="javascript">

<html>

   <body onLoad="alert("The document has completely loaded.")">

</html></source>


window.onMove

The onMove event handler is fired when the window it is referenced in is moved.



   <source lang="javascript">

<html>

   <body onMove="alert("Do NOT move this window!")">

</html></source>


window.onResize

The onResize event handler is fired when the window is resized.



   <source lang="javascript">

<html>

   <body onResize="alert("Do NOT resize this window!")">

</html></source>


window.onUnLoad

The onUnLoad event handler is fired when the page is unloaded. This occurs when the user leaves the page for another page.

The onUnLoad event handler in a document will fire before an event handler loaded in the <frameset> tag.



   <source lang="javascript">

<html>

   <body onUnLoad="alert("Please do not leave!")">

</html></source>


window.open()

Syntax



   <source lang="javascript">

window.open(pageURL, name, parameters)</source>


The open() method creates a new instance of a window. It loads the pageURL. The ACTION attribute of the <form> tag and the TARGET attribute of the <a> tag can reference the window.

You must use comma to separate each of these options and do not insert any spaces.

Parameters That Can Be Passed When Creating a New Window

Parameter Initialize With Description alwaysLowered yes/no tells the window to stay behind all other windows. This must be done in signed scripts. alwaysRaised yes/no tells the window to stay on top of all other windows. This must be done in signed scripts. dependent yes/no opens the window as a true child window of the parent window. directories yes/no Specifies if the Directory Bar on Navigator 2 and 3 is visible. height pixel value Sets the height of the window. hotkeys yes/no Disables all but the Security and Quit hotkeys in a new window with no Menu Bar. innerHeight pixel value Sets the height of the document. innerWidth pixel value Sets the width of the document. location yes/no Specifies if the Location Bar is visible. menubar yes/no Specifies if the Menu Bar is visible. outerHeight pixel value Sets the height of the window, including the chrome. outerWidth pixel value Sets the width of the window, including the chrome. resizable yes/no Specifies if the window can be resized. screenX pixel value Sets the distance of the window from the left side of the screen. screenY pixel value Sets the distance of the window from the top of the screen. scrollbars yes/no Specifies if the Scroll Bars are visible. titlebar yes/no Specifies if the Title Bar is visible. toolbar yes/no Specifies if the toolbar is visible. width pixel value Sets the width of the window. z-lock yes/no Specifies that the window is not supposed to be located above other windows when it is made active.

window.opener

Syntax



   <source lang="javascript">

window.opener

   window.opener.method
   window.opener.property</source>
   
  

window.outerHeight

The outerHeight property references the pixel height of the browser"s frame.

This includes any of the toolbars or other "chrome" that makes up the frame itself.



   <source lang="javascript">

<html>

   <head>
   <script language="JavaScript1.2">
   
   </script>
   </head>
   <body>
   <form>
     <input type=BUTTON value="Open" onClick="openWin()">
   </form>
   </body>
   </html></source>
   
  

window.outerWidth

The outerWidth property references the pixel width of the browser"s frame.

This includes any of the toolbars or other "chrome" that make up the frame itself.



   <source lang="javascript">

<html>

   <head>
   <script language="JavaScript1.2">
   
   </script>
   </head>
   <body>
   <form>
     <input type=BUTTON value="Open" onClick="openWin()">
   </form>
   </body>
   </html></source>
   
  

window.pageXOffset

The pageXOffSet property reflects the current horizontal pixel location of the top-left corner of the document.



   <source lang="javascript">

<html>

   <head>
   <script language="JavaScript1.2">
   
   </script>
   </head>
   <body>
   <form>
     <input type=BUTTON value="Show Location" onClick="showLocation()">
   </form>
   </body>
   </html></source>
   
  

window.pageYOffset

The pageYOffSet property reflects the current vertical pixel location of the top-left corner of the document.



   <source lang="javascript">

<html>

   <head>
   <script language="JavaScript1.2">
   
   </script>
   </head>
   <body>
   <form>
     <input type=BUTTON value="Show Location" onClick="showLocation()">
   </form>
   </body>
   </html></source>
   
  

window.parent

Syntax



   <source lang="javascript">

window.parent.frames[num]

   window.parent.frameName</source>
   
  

window.personalbar

Syntax



   <source lang="javascript">

window.personalbar.property</source>


window.print()

The print() method simulates the user clicking the Print button on the browser.

It tells the browser to open the print dialog box to print the current page.



   <source lang="javascript">

<html>

   <body>
   <form>
     <input type=BUTTON value="Print" onClick="window.print()">
   </form>
   </body>
   </html></source>
   
  

window.prompt()

Syntax



   <source lang="javascript">

window.prompt(string1, string2)</source>


window.releaseEvents()

Syntax



   <source lang="javascript">

window.releaseEvents(event)

   window.releaseEvents(event1 | event2 | eventN)</source>
   
  

window.resizeBy()

Syntax



   <source lang="javascript">

window.resizeBy(numHort, numVert)</source>


window.resizeTo()

Syntax



   <source lang="javascript">

window.resizeTo(numWidth, numHeight)</source>


window.routeEvent()

Syntax



   <source lang="javascript">

window.routeEvent(event)</source>


window.scroll()

Syntax



   <source lang="javascript">

window.scroll(numX, numY)</source>


window.scrollbars

Syntax



   <source lang="javascript">

window.scrollbars.property</source>


window.scrollBy()

Syntax



   <source lang="javascript">

window.scrollBy(numHort, numVert)</source>


window.scrollTo()

Syntax



   <source lang="javascript">

window.scrollTo(numX, numY)</source>


window.self

Syntax



   <source lang="javascript">

window.self.method

   window.self.property</source>
   
  

window.setInterval()

Syntax



   <source lang="javascript">

window.setInterval(expression, milliseconds)

   window.setInterval(function, milliseconds)
   window.setInterval(function, milliseconds, arg1, ..., argN)</source>
   
  

window.status

Syntax



   <source lang="javascript">

window.status = string</source>


window.statusbar

Syntax



   <source lang="javascript">

window.statusbar.property</source>


window.stop()

Syntax



   <source lang="javascript">

window.stop()</source>


window.toolbar

Syntax



   <source lang="javascript">

window.toolbar.property</source>


window.top

Syntax



   <source lang="javascript">

window.top.frames[num]

   window.top.frameName
   window.top.method
   window.top.property</source>
   
  

The top property contains a reference to the topmost browser window of any frames or pages.