Methods to close an open window: 1. Use "window.close" to close the new window; 2. Use "window.location" to redirect; 3. Use "window.blur" and "window.focus" ; 4. Use "window.history" to roll back; 5. Use "window.opener".
window.open is a function in JavaScript that opens a new window or tab. However, due to browser security restrictions, you cannot directly return to the original window or tab from a new window or tab, nor can you directly close the new window or tab. This is to prevent malicious scripts from closing windows or tabs without the user's knowledge.
However, there are ways you can close new windows or tabs indirectly. Here are several possible methods:
1. Use window.close to close the new window
Although you cannot directly close the new window opened by window.open, You can call the window.close function on a new window to close it. For example:
window.open('https://www.example.com', '_blank'); // 在新窗口中... window.close();
Please note that only windows opened by window.open can be closed. Browsers generally do not allow scripts to close windows that were not opened by scripts. Additionally, browsers may prevent a script from closing a window containing window.close unless the script is run within the window's sandboxed environment or the window was opened via window.open .
2. Use window.location redirection
You can redirect to a new page by entering a new URL in the address bar of the new window, thereby indirectly Close the new window. For example:
window.open('https://www.example.com', '_blank'); // 在新窗口中... window.location = 'https://www.example2.com';
In this example, the new window will be redirected to the new URL, thereby indirectly closing the original window. Note that this approach may create a bad experience for the user because it changes the browser's focus.
3. Using window.blur and window.focus
You can try to make the new window lose focus and then regain focus, which may close it. For example:
window.open('https://www.example.com', '_blank'); // 在新窗口中... window.blur(); // 失去焦点 setTimeout(function() { window.focus(); // 重新获得焦点 }, 0);
This approach may not always work as browser implementations may vary. Additionally, this approach may create a bad experience for the user because it changes the browser's focus.
4. Use window.history to go back
You can try to use the back() method of the window.history object to go back to the previous history entry. This may close the new window or tab. For example:
window.open('https://www.example.com', '_blank'); // 在新窗口中... window.history.back();
Please note that this approach may create a bad experience for the user as it changes the browser history. Additionally, this approach may not always work, as browser implementations may vary.
5. Use window.opener
If you opened it in a new window, you can use the window.opener property to access the original window. You can then close the new window by calling window.close() on the original window. For example:
In the original window:
var newWindow = window.open('https://www.example.com', '_blank'); // 在新窗口中... newWindow.close(); // 关闭新窗口
Please note that this approach may create a bad experience for the user as it requires the user to manually close the new window. Additionally, this method may not always work due to browser security restrictions.
The above is the detailed content of window.openHow to close an open window. For more information, please follow other related articles on the PHP Chinese website!