Check whether a window exists
When you create a new window, it is very important to assign a variable to the return value of the open () method. For example, the following statement creates a new
Window, and close it immediately:
Win = window. open ("http://www.docjs.com/", "js ");
Win. close ();
Window object
Each browser window corresponds to a window object. Therefore, when you want to reference the current window, you should use the window object. Lower
The following statement sets the URL of the current window:
Window. location. href = "http://www.docjs.com /";
When you place such a statement in a script program, you do not need to specify the window object, because the current window exists by default:
Location. href = "http://www.docjs.com /";
Note that self is equivalent to window. Therefore, self. close () is actually equal to window. close ().
When you want to manipulate a window, you must confirm that it exists.
When defining a variable for the window. open () method, define it as a global variable and set it to null. Remember, the open () method returns
Return the window object of the new window. Here is an example:
Var win = null;
Function launchWindow (){
Win = window. open ();
// Statements that refer to the new window go here
}
If you want to perform an operation on the new window, you should first check whether the variable win is null:
// If win exists, move the window
If (win) win. moveTo (0, 0 );
Open "attribute"
Now you know that the browser actually creates a new window, but does it still exist? You need to confirm that the available window does have a real windo
W object. Since each window object corresponds to an open () method, you can check this method through object detection:
// If win. open exists, move the window
If (win. open) win. moveTo (0, 0 );
Note that the conditional expression is a function reference, not a function call. Also, unless you confirm that win exists, you should not try to estimate the wi
N. open. The following statement describes the correct execution method:
// If win and win. open exist, move the window
If (win & win. open) win. moveTo (0, 0 );
Because & is a short operator symbol, if the first parameter (win) corresponds to true, The result depends on the second value. If the second
If the parameter is false, the result of the entire expression is false. This is a very important behavior, because if win does not exist, then the expression wi
N. open will generate an error.
Closed attributes
Some methods of a window object, such as close (), can be executed even when the window is closed, while others cannot
For example, moveTo ()). However, Internet Explorer and Navigator often have different behaviors. In addition, predict the completion of certain operations
It is often very difficult, even if you have tried something similar before. We will show you how to overcome these difficulties by introducing
The statement that can be applied in the cross browser. It checks whether a given window is opened.
The window. closed attribute is a Boolean value that defines whether the window has been closed. When the window is closed, the window object
And its closed attribute is set to true.
Use closed to determine whether the window to be opened and whether the window that can still be referenced (the value returned from the window. open method) is still open
. When the window is closed, you should not try to manipulate it any more. Because window. closed is only used by Internet Explorer 4, Navigator 3 and
High Version is supported, so you should pay attention to the previous version issues. We will use the following code:
// If win and win. open exist, and win. closed isn' t true, move the window
If (win & win. open &&! Win. closed) win. moveTo (0, 0 );
Internet Explorer 3 and Navigator 2 do not support the closed method. Therefore, the value of closed in a Boolean expression is equal to false (
Like other non-existent attributes, such as window. tomershiran ).