50分求一段判断有几个父层的JS代码

luohuayh 2010-03-09 03:37:01
如题,用window.open()打开窗口,但不知打开几个,如何判断当前窗口是第几个窗口用window.open()方法打开的?
要求不能用服务器语言,只能用JavaScript语言
能写出代码的给满分
...全文
142 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
luohuayh 2010-03-10
  • 打赏
  • 举报
回复
引用 12 楼 free_wind22 的回复:
HTML code<!DOCTYPE html><htmlxmlns="http://www.w3.org/1999/xhtml"><head><metahttp-equiv="Content-Type" content="text/html; charset=gb2312"/><title>无标题文档</title></head><body><scripttype="text/javascrip?-

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<script type="text/javascript">
var pWin = window;
window.onload = function(){
pWin = window.opener ? window.opener.getTopWindow() : pWin;
};
function getTopWindow(){
return pWin;
}
function openWin(){
window.open(window.location.href);
if(window.opener)
{
window.close();
}
}
function show(){
getTopWindow().document.getElementById("t").innerHTML = new Date().toLocaleString();
}
</script>
<div id="t"></div>
<input type="button" value=" 新建窗口 " onClick="openWin()" />
<input type="button" value=" 第一个窗口刷新时间 " onClick="show()" />
</body>
</html>

我说的是这种情况?
2010-03-10
  • 打赏
  • 举报
回复

<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<script type="text/javascript">
var pWin = window;
window.onload = function(){
pWin = window.opener ? window.opener.getTopWindow() : pWin;
};
function getTopWindow(){
return pWin;
}
function openWin(){
window.open(window.location.href);
}
function show(){
getTopWindow().document.getElementById("t").innerHTML = new Date().toLocaleString();
}
</script>
<div id="t"></div>
<input type="button" value=" 新建窗口 " onClick="openWin()" />
<input type="button" value=" 第一个窗口刷新时间 " onClick="show()" />
</body>
</html>
luohuayh 2010-03-10
  • 打赏
  • 举报
回复
引用 10 楼 wdbjsh 的回复:
不建议多个open之间的操作,在java和。net的应用中,这个opener的引用不正常,很多bug

这个问题我知道,但这我也是无奈之举,并不是说我不想用就不用的问题,现在就是如何去判断open完后他的前一页面是否被关闭
wdbjsh 2010-03-10
  • 打赏
  • 举报
回复
不建议多个open之间的操作,在java和。net的应用中,这个opener的引用不正常,很多bug
luohuayh 2010-03-10
  • 打赏
  • 举报
回复
7#的方法是不错,但唯一的缺陷就如我所说的就是期间有一个窗口是在用window.open()打开一个新窗口后又用window.close()关闭本窗口时,那样就找不到父层,所以还是没办法实现我说的功能
luohuayh 2010-03-10
  • 打赏
  • 举报
回复
要是期间有一个窗口是在用window.open()打开后又用window.close()关闭的,那么你这段代码还有效吗?
luohuayh 2010-03-10
  • 打赏
  • 举报
回复
谢谢你帮我解决了这个难题,50分全给你。
2010-03-10
  • 打赏
  • 举报
回复
就是当前执行的函数,你alert一下就知道了
luohuayh 2010-03-10
  • 打赏
  • 举报
回复
arguments.callee这个我用的比较少,他的作用是什么呢?
2010-03-10
  • 打赏
  • 举报
回复
改成这样呢?


<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<script type="text/javascript">
var pWin = window.opener ? window.opener.getTopWindow() : window;

function getTopWindow(){
return pWin;
}
function openWin(){
var win = window.open(window.location.href);
(function(){
if(win.getTopWindow){
window.close();
}else{
setTimeout(arguments.callee, 100);
}
})();
}
function show(){
getTopWindow().document.getElementById("t").innerHTML = new Date().toLocaleString();
}
</script>
<div id="t"></div>
<input type="button" value=" 新建窗口 " onClick="openWin()" />
<input type="button" value=" 第一个窗口刷新时间 " onClick="show()" />
</body>
</html>
2010-03-09
  • 打赏
  • 举报
回复

<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<script type="text/javascript">
function openWin(){
window.open(window.location.href);
}
function show(){
var obj = window;
while(obj.opener){
obj = obj.opener;
}
obj.document.getElementById("t").innerHTML = new Date().toLocaleString();
}
</script>
<div id="t"></div>
<input type="button" value=" 新建窗口 " onclick="openWin()" />
<input type="button" value=" 第一个窗口刷新时间 " onclick="show()" />
</body>
</html>
luohuayh 2010-03-09
  • 打赏
  • 举报
回复
我要的是这样一段代码,怎么个说呢,就是说那些页面都是已经存在的了,而不是说要我从新去写那个打开方法,现在所需要的就是重新写一段代码到公用类里,然后调用来判断当前窗口是在哪个父层下,从而来知道要用几个opener来调用第一个弹出窗口的父窗口的方法,比如说有两个弹出窗口就用opener.opener.window...来调用,有三个弹出窗口就用三个opener,现在这么说不知道你能不能明白
2010-03-09
  • 打赏
  • 举报
回复

<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<script type="text/javascript">
var nIndex = 1;
window.onload = function(){
nIndex = window.opener ? window.opener.getIndex() + 1 : 1;
document.getElementById("t").innerHTML = getIndex();
};
function getIndex(){
return nIndex;
};
function openWin(){
window.open(window.location.href);
}
</script>
<div id="t"></div>
<input type="button" value=" Open " onclick="openWin()" />
</body>
</html>
passself 2010-03-09
  • 打赏
  • 举报
回复

<!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>
<script>
function myRef(){
window.open(''+self.location,'mywin','left=20,top=20,width=500,height=500,toolbar=1,resizable=0');
myRef.focus()
}
</script>
</head>

<body>
<form>
<input type=button value="Open new window" onClick="myRef()" />
</form>
</body>
</html>


楼主做剩下的部分
shan1119 2010-03-09
  • 打赏
  • 举报
回复
index.html:
<script>
var index=window.location.search.split("=")[1]|0;
document.write(index);
function openWin(){
window.open("photo1.html?index=" +(index+1),"_blank");
}
</script>
<a href="javascript:openWin();">open<a/>

photo1.html:

<script>
var index=window.location.search.split("=")[1]|0;
document.write(index);
function openWin(){
window.open("photo2.html?index=" +(index+1),"_blank");
}
</script>
<a href="javascript:openWin();">open<a/>


..
..
..
luohuayh 2010-03-09
  • 打赏
  • 举报
回复
看来楼上老兄有点误解我的意思了,我说的意思不是说在这个页面上点出几个弹出窗口,而是第一个窗口是用open方法打开的,而第二窗口是在笨窗口用open打开的,而第三个窗口又是在第二个窗口用open打开的,依次类推,但期间有些窗口可能是在打开的时候就被关闭了,所以不是你的那个计数的方法
shan1119 2010-03-09
  • 打赏
  • 举报
回复
test.html

<script type="text/javascript">
var i=1;
function openWin(){
window.open("photo1.html?index="+(i++),"_blank");
}
</script>
<a href="javascript:openWin();">open<a/>


photo1.html
<script>
document.write(window.location.search.split("=")[1]);
</script>

87,997

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧