JavaScript FAQ (eight)--window (Windows)

Source: Internet
Author: User

VI. Window

1. Open a window (Opening a Windows)

Q: How do I open a new browser window?

a: You can use the () method to open a new browser window. For example, the following code displays the current page in a new window.

The general syntax for the () method is as follows:

Winref = (URL, name [, features [, replace]])

The return value stored in the variable winref is a reference to the new window. You can use this reference later, for example, close the window (Winref.close ()), position the focus to a window (Winref.focus ()), or perform another window operation.

The meaning of the parameter URL, name, feature, and replace is:

URL Sring, which specifies the address of the page to open in the new window. If you don't want to specify an address, you can pass an empty string (such as when you want to write something from the script in a new window).

name String that specifies the name of the new window. The use of this name is the same as the frame name in the frame. For example, you can use <a traget=name fref= "Page.htm" in a hyperlink in a form, and the destination page for the hyperlink will be displayed in a new window.

If a window of the same name already exists, then () displays the new content in the existing window instead of creating a new window.

features String, an optional parameter that specifies the attributes of the new window. The features string may contain one or more feature=name pairs separated by commas.

Replace boolean, optional argument. If true, the new content is replaced with the current page in the browser navigation history. Note: Some browsers will ignore this parameter.

The following features are available on most browsers:

toolbar = 0|1 Specifies whether the toolbar is displayed in a new window.

Location = 0|1 Specifies whether the address bar is displayed in a new window.

directories = 0|1 Specifies whether Netscape's Folder button is displayed in the new window.

Status = 0|1 Specifies whether the status bar is displayed in a new window.

menubar = 0|1 Specifies whether the browser menu bar is displayed in a new window.

ScrollBar = 0|1 Specifies whether scroll bars should be in a new window.

resizable = 0|1 Specifies whether a new window can reset the size.

width = pixels Specifies the widths of new windows.

Height = pixels Specifies the height of the new window.

top = pixels Specifies the y-coordinate of the upper-left corner of the new window (not supported in version 3 browsers).

left = pixels Specifies the x-coordinate (not supported in version 3 browsers) of the upper-right corner of the new window.

2. Close Windows (Closing a window)

Q: How do I close a window.

A: To close a window that was previously opened through a script, you can use the Window.close () method, for example:

Winref.close ();

The winref above is a window reference returned by the () method. The gotcha here is that when your script tries to close the Winref application window, it must exist. (the user may have been turned off.) Then if the window no longer exists, the script will have an error. Possible solutions are: reopen the same winref and name window to place the closing window code only where the window is closed. In this case, the window reference is itself (self), that is, Self.close (). Test whether the window is still open before attempting to close.

If your script tries to close the main window opened by the user, most browsers will pop up a window asking the user if they want to allow the script to close the browser window. You test yourself: This is a script that tries to close your browser window.

3. Reset Window Size (resizing a Windows)

Q: How do I reset the window size.

a: on Netscape Navigator 4 or Internet Explorer 4 (Translator Note: and later), you can use Window.resizeto (Newwidth, Newheight) or the Window.resizeby (DX, DY) method resets the window size.

Note that the previous versions of browsers do not support these methods. The best solution would be to "do nothing on an older browser". For example, the following code, which is browsed in a new version, resets the window to 600x400 and does not cause an error in the previous version:

if (parseint (navigator.appversion) >3)
Top.resizeto (600,400);

Note that the parameters of Resizeto () have different meanings in different browsers: they specify an outer window size on Internet Explorer, and in Netscape Navigator specify the size of the inner window (excluding window borders, toolbars, status bars, Title bar and Address bar). You can use this function if you want to reset to the same outer window size on all browsers:

function Resizeouterto (w,h) {
 if (parseint (navigator.appversion) >3) {
   if (navigator.appname== "Netscape") {
   else Top.resizeto (w,h);

4. Maximize Windows (maximizing a window)

Q: How do I maximize the window.

A: To maximize the window, your code should first determine the available screen size, then reset the window to the user screen size. Note that there is no reliable way to detect the screen size in the mainstream browser of version 3 (in addition to invoking Java from Navigator 3.x). Therefore, the following example function Maximizewindow () can only work on 4 or newer versions. Try this:

Source code is:

function Maximizewindow () {
 if (parseint (navigator.appversion) >3) {
  if (navigator.appname== "Netscape") {
   if (top.screenx>0 | | top.screeny>0) top.moveto (0,0);
   if (Top.outerwidth < screen.availwidth)
   if (Top.outerheight < screen.availheight) 
  else {
   Top.moveto ( -4,-4);
   Top.resizeto (screen.availwidth+8,screen.availheight+8);}}

Two note:

1. in Windows, maximizing a window is the equivalent of moving the upper-left corner of the window to point x=-4, y=-4 and resetting the window size to screen size plus 8 pixels (in horizontal and vertical two dimensions)

This hides the border of the window. Unfortunately, Netscape Navigator4 requires a signed script to move the top-left corner of the window away from the screen, so the result of the above (unsigned) script is that the window border is clearly visible. (When you change the window, Netscape's associated branch script detects whether the window has been maximized.) If so, leave it intact.

2. It is also necessary to note that JavaScript code cannot change the style of the maximized button (the second button in the upper-right corner).

5. Write Windows (writing to a window)

Q: How do I write the script-generated content to another window?

A: You can use the WinRef.document.write () or WinRef.document.writeln () method to write the script-generated content to another window. For example, the following function opens a new window with the title Console , and then writes the specified content to it.

Writeconsole (' Hello from javascript! ');
function Writeconsole (content) { (', ' myconsole ', '
  width=350,height=250 '
   + ', menubar=0 '
   + ', toolbar=1 ' + '
   , status=0 '
   + ', Scrollbars=1 '
   + ', resizable=1 ')
 Top.consoleRef.document.writeln (

In the above example, you may have noticed that after writing to the console several times, the console window allows you to go back and forth in the history of the output. This is not a function that has always been needed. If you want to output the content without creating a history, put the following action after the window is open (and before the first write):
Docref = ("text/html", "replace");
The winref here is a reference to the window returned by the () method, docref a global variable, as a reference to the new document by the script.

6. Is my window still there? (Is my window still open?)

Q: How do I test if my window is still there?

a: Let's say you opened a new browser window using the () method

Winref = (URL, name, features)
You can then use the Window.closed property to test whether the window is still open:

if (winref.closed) alert ("It ' s closed!")
else alert ("It ' s still open!")

The Window.closed property is not supported in Netscape Navigator 2 or Internet Explore 3. To avoid error messages, you can put the above code in a conditional statement, as follows:

if (parseint (navigator.appversion>2)) {
if (winref.closed) alert ("It ' s closed!")
else alert ("It ' s Still open!")

(Internet Explore 3 reports that its version is 2, so this navigator.appversion condition is true for Navigator 3 or higher and Internet Explorer 4 or higher.) )

There is no simple workaround for an older version of the browser. You may want to use the OnUnload event handler to simulate the window.closed attribute. However, it is important to note that the Unload event is not equivalent to closing a window. For example, it also triggers the event when a user leaves the original page to another page (and the window is still open).

7. Window title (Windows title)

Q: How do I change the title of another window, which is the contents of the title bar at the top of the window.

A: In order to change the window caption, your script writes the string of the window to begin with the following:

' Note that overriding the contents of other Windows erases everything that was previously displayed in the window. Therefore, if you want the contents of the window to still exist, then, after changing the window title, you need to rewrite the contents of the window once. (If your script doesn't know what the old content is, then you don't change the title of the window at the end.) )

For more information about writing script-generated content to Windows, refer to the Write window.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.