Sometimes, we want to use JS to achieve a <a> new open label effect, at this time we think of window.open method implementation. So what should window.open do with it? We know that window.open can open a new window or tab (which is usually related to the browser's setup options), load the specified URL into a new or existing window, and return the Window object that represents that one. Window.close can close the window, but only automatically close the JS open window, if you want to close other windows, you must prompt the user to determine or cancel the operation (this browser-related, the test IE needs to be prompted to close; other browsers do not allow automatic shutdown). We can open the window directly using window.open () and close the window with Window.close ()?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<!DOCTYPE html> <meta charset=
"utf-8"
> <title>window.open和window.close的使用详解</title> <body> <button id=
"openWindow"
>打开窗口</button> <button id=
"closeWindow"
>关闭窗口</button> <script> (
function
(){
var $ =
function
(id) {
return document.getElementById(id);}
var win =
null
;
//打开窗口
$(
‘openWindow‘
).onclick =
function
() {
win = window.open();
}
//关闭窗口
$(
‘closeWindow‘
).onclick =
function
() {
win && win.close();
}
//自动关闭窗口
window.close(); })(); </script> </body>
|
window.open (URL, name[, property][, Boolean]) requires four parameters: first parameter URL: The URL of the document displayed in the new window, if omitted or empty, ' About:blank ' is used The second parameter name: The name of the new window, can be used with the target, if omitted, will use ' _blank ' (here is a brief introduction: _parent and _top,_parent refers to the Direct parent window, _ Top refers to the top-level ancestor window) The third parameter, Protery, is a comma-separated optional parameter that contains the window size and various properties. If omitted, then the default size and standard UI component (explicit invocation is more like opening the window than the label). This parameter is non-standard, and the HTML5 specification also argues that the browser ignores it. The fourth parameter, Boolean: only valid if the second parameter is named a window that exists. Declares the URL specified by the first parameter, replacing the current entry for the window's browsing history. Window.opener refers to the original window, if not, null.?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<!DOCTYPE html> <meta charset=
"utf-8"
> <title>window.open和window.close的使用详解</title> <body> <button id=
"openWindow"
>打开窗口</button> <button id=
"closeWindow"
>关闭窗口</button> <script> (
function
(){
var $ =
function
(id) {
return document.getElementById(id);}
var win =
null
;
//打开窗口
$(
‘openWindow‘
).onclick =
function
() {
win = window.open(
‘about:blank‘
,
‘_blank‘
,
‘width=200,height=200‘
);
//win.opener指的是新窗口原始窗口
//alert(win.opener === window);
}
//关闭窗口
$(
‘closeWindow‘
).onclick =
function
() {
win && win.close();
//主动关闭窗口后win.opener为false
//alert(win.opener === window);
}
//alert(window.opener === window);
//自动关闭窗口
//window.close(); })(); </script> </body>
|