标签:
<!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>
<!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/xiaoheimiaoer/p/4734977.html