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 closes it immediately:
Win = Window. Open ("http://www.docjs.com/", "JS ");
Win. Close ();
Window object
Each browser window corresponds to a clear window object. Therefore, when you want to reference the current window, you should use the window object. The following statement sets the URL of the current window:
Window. Location. href = "http://www.docjs.com /";
When you are in the scriptProgramWhen such a statement is placed in, you do not need to specify the window object, because the existence of the current window is the 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 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 );
Note that null is equal to false, and any other valid object is equivalent to true. If win is equal to true, you will know that it is not null, which means that the new window is successfully created.
Open "attribute"
Now you know that the browser actually creates a new window. But does it still exist? Not required. Make sure that the available window does have a real window 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 );
The variable win matches the window object of the window, so win. Open corresponds to the window. Open Method of window. Note that a conditional expression is a function reference rather than a function call. Unless you confirm that win exists, you should not try to estimate win. 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 parameter is false, the result of the entire expression is false. This is a very important behavior, because if win does not exist, the expression win. Open will produce an error.
Closed attributes
Because of the first version of JavaScript, browser windows become a very difficult issue. For example, some methods of a window object, such as close (), can be executed even when the window is closed, and others cannot (such as moveTo ()). But the situation will be worse. Internet Explorer and navigator often have different behaviors. Moreover, it is often very difficult to predict the results of certain operations, even if you have tried similar operations before. We will show you how to overcome these difficulties by introducing a statement that can be applied in a cross-browser and checking whether a given window is opened.
The window. Closed attribute is a Boolean value that defines whether the window has been closed. When a window is closed, the window object still exists and its closed attribute has been set to true.
Use closed to determine whether the opened window and the window that can still be referenced (the value returned from the window. Open method) are still open. When the window is closed, you should not try to manipulate it any more. Because window. Closed is only supported by Internet Explorer 4, navigator 3 and later versions, you should pay attention to the previous version. We will use the followingCode:
// 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, so it is moderately priced at false in a Boolean expression (just like other properties that do not exist, such as window. tomershiran ).