有三个页面Main,Sub,Sub-sub, 打开Main时会弹出Sub, Sub加载完毕会弹出Sub-sub, 而中间窗口Sub页面加载完毕会直接调用window.close()关掉, Main里面有个js的方法T(),需要在第三个窗口Sub-Sub调用。
现在的问题是:把Sub页面的window.close()注释掉的话在主流浏览器IE7,8,9 Chrome9.0 FireFox3.6 Safari5.0.3中都正常,但中间页面要是调用window.close()的话第三个页面仅在IE和Chrome中能调用到T()方法。还请各位大侠指教,不甚感激。
如果我的解释不够清楚的话请看这里:传送门三个页面关键代码如下:
Main.htm: <script type="text/javascript">
var win = window.open('Sub.htm', '', 'height=600, width=800, top=200, left=200, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
function T() {
alert("top level page");
}
</script> <input type="text" value="value from the top level page" />
Sub.htm: <script type="text/javascript">
var win = window.open('', '', 'height=600, width=800, top=200, left=200, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
win.location.href = 'Sub-sub.htm';
win.opener = window.opener; //pass the opener as the reference.
//window.close();
</script>
Sub-sub.htm: <script type="text/javascript"> function Browser() { var ua, s, i; this.isIE = false;
this.isNS = false;
this.version = null; ua = navigator.userAgent; s = "MSIE";
if ((i = ua.indexOf(s)) >= 0) {
this.isIE = true;
this.version = parseFloat(ua.substr(i + s.length));
return;
} s = "Netscape6/";
if ((i = ua.indexOf(s)) >= 0) {
this.isNS = true;
this.version = parseFloat(ua.substr(i + s.length));
return;
} // Treat any other "Gecko" browser as NS 6.1.
s = "Gecko";
if ((i = ua.indexOf(s)) >= 0) {
this.isNS = true;
this.version = 6.1;
return;
}
}
var browser = new Browser(); var refToMain;
if (browser.isIE)
//This only works for IE browsers.
refToMain = window.opener.parent;
if (browser.isNS)
//This only works for non-IE browsers.
refToMain = window.opener.opener; alert(refToMain.document.getElementsByTagName("input")[0].value); function CallTop() {
refToMain.T();
} </script> <input type="button" value="invoke the top page" onclick="CallTop()" />
现在的问题是:把Sub页面的window.close()注释掉的话在主流浏览器IE7,8,9 Chrome9.0 FireFox3.6 Safari5.0.3中都正常,但中间页面要是调用window.close()的话第三个页面仅在IE和Chrome中能调用到T()方法。还请各位大侠指教,不甚感激。
如果我的解释不够清楚的话请看这里:传送门三个页面关键代码如下:
Main.htm: <script type="text/javascript">
var win = window.open('Sub.htm', '', 'height=600, width=800, top=200, left=200, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
function T() {
alert("top level page");
}
</script> <input type="text" value="value from the top level page" />
Sub.htm: <script type="text/javascript">
var win = window.open('', '', 'height=600, width=800, top=200, left=200, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
win.location.href = 'Sub-sub.htm';
win.opener = window.opener; //pass the opener as the reference.
//window.close();
</script>
Sub-sub.htm: <script type="text/javascript"> function Browser() { var ua, s, i; this.isIE = false;
this.isNS = false;
this.version = null; ua = navigator.userAgent; s = "MSIE";
if ((i = ua.indexOf(s)) >= 0) {
this.isIE = true;
this.version = parseFloat(ua.substr(i + s.length));
return;
} s = "Netscape6/";
if ((i = ua.indexOf(s)) >= 0) {
this.isNS = true;
this.version = parseFloat(ua.substr(i + s.length));
return;
} // Treat any other "Gecko" browser as NS 6.1.
s = "Gecko";
if ((i = ua.indexOf(s)) >= 0) {
this.isNS = true;
this.version = 6.1;
return;
}
}
var browser = new Browser(); var refToMain;
if (browser.isIE)
//This only works for IE browsers.
refToMain = window.opener.parent;
if (browser.isNS)
//This only works for non-IE browsers.
refToMain = window.opener.opener; alert(refToMain.document.getElementsByTagName("input")[0].value); function CallTop() {
refToMain.T();
} </script> <input type="button" value="invoke the top page" onclick="CallTop()" />
var win = window.open('', '', 'height=600, width=800, top=200, left=200, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
win.location.href = 'Sub-sub.htm';
win.opener = window.opener; //pass the opener as the reference.
//window.close();
</script>你这么做,应该想到原因啊。其它浏览器拒绝了 win.opener = window.opener; 这种欺骗。它们不喜欢这种小手脚。
但是以当前的代码,所有浏览器都能正常运行。
我的问题是:把//window.close();去掉注释的话,中间窗口一关,在部分浏览器,第三个窗口就调用不到第一个窗口的方法了,貌似是中间联系给断掉了。而IE和Google依然可以正常运行。
而window.close()必须调用,以满足需求。
具体的问题请到我提供的传送门里面瞧瞧。
是,这样确实好实现,不过客户需求里面是需要中间那个页面的,这个只是从项目中分离出来相关代码写的一个简单的Demo来研究这个问题。
需求必须在Sub页中关掉,见原帖。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<script type="text/javascript">
var win = window.open('Sub.html', 'Sub', 'height=600, width=800, top=200, left=200, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
function T() {
alert("top level page");
}
</script>
<input type="text" value="value from the top level page" />
</body>
</html>
Sub.html<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<script type="text/javascript">
var closeIt = 0;
var win = window.open('Sub-sub.html', 'Sub_sub', 'height=600, width=800, top=200, left=200, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
(function() {
if(closeIt) {
window.close();
} else {
setTimeout(arguments.callee, 10);
}
})();
</script>
</body>
</html>
Sub-sub.html<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<script type="text/javascript">
var p = window.opener.opener; alert(p.document.getElementsByTagName("input")[0].value); window.opener.closeIt = 1;
function CallTop() {
p.T();
}
</script>
<input type="button" value="invoke the top page" onclick="CallTop()" />
</body>
</html>