今天再次谈起window.open是因为发现了一个比较好玩的小技巧,详细内容我们稍后详细说明。
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
32
33
34
35
36
37
38
39
40
41
|
<! DOCTYPE > < html > < head > < meta charset="utf-8"> < title >window.open小技巧</ title > </ head > < body > 我是窗口:window.open < button id="openWindow">点击新开窗口</ button > < script type="text/javascript"> (function() { var openWindowEl = document.getElementById(‘openWindow‘); var newWindow; var data; window.windowName = ‘window.open‘; openWindowEl.onclick = function() { newWindow = window.open(‘window.close.php‘, ‘_blank‘, ‘width=500,height=500;‘); //需要window.close.html js正常执行之后才行 /*setTimeout(function() { console.log(newWindow.userName); }, 1000);*/ newWindow.onbeforeunload = function() { userName = newWindow.windowName; data = newWindow.data; } newWindow.onunload = function() { alert(userName); if(data.errno == 0) { alert(data.errmsg); }else { alert(data.errmsg); } } }; })(); </ script > </ body > </ html > |
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
32
33
|
<! DOCTYPE > < html > < head > < meta charset="utf-8"> < title >window新开窗口</ title > </ head > < body > 我是窗口:window.close < script type="text/javascript"> (function() { window.windowName = ‘window.close‘; if(Math.random() > 0.5) { window.data = { errno: 0, errmsg: ‘成功~‘ }; }else { window.data = { errno: 1, errmsg: ‘数据异常~‘ }; } setTimeout(function() { //拿到的是父窗口的window alert(window.opener.windowName); }, 100); setTimeout(function() { window.close(); }, 1000); })(); </ script > </ body > </ html > |
原文:http://www.cnblogs.com/softidea/p/4735203.html