window.open 全局范围变量 javascript
谁能告诉我如何使用全局范围变量,以便我可以从“窗口2”关闭“窗口1”?这真的让我陷入困境,所以任何帮助都会很棒!
我有index.html,有 1 个链接和 1 个按钮。按钮打开窗口 1,链接打开窗口 2。我希望窗口 2 有一个可以关闭窗口 1 的按钮。如果可以的话请帮忙!!谢谢!!!
<script type="text/javascript">
function openWin1()
{
myWindow=window.open('','','');
myWindow.document.write("<p><img src=\"flower.jpg\" /></p>");
myWindow.focus();
}
function openWin2()
{
myWindow=window.open('','','');
myWindow.document.write("<style type=\"text/css\">body{background-color:yellow;}</style>
<p><img src=\"bee.jpg\" /></p><input type=\"button\" onclick=\"window.close();\"
value=\"Close Window 1\" />");
myWindow.focus();
}
</script>
</head>
<body>
<input type="button" value="Window 1" onclick="openWin1();" /><br />
<a href="javascript:openWin2();">Window 2</a>
</body>
</html>
Can anyone tell me how i could use a global scope variable so that I could close "window 1" from "window2"?This is really throwing me for a loop, so any help would be great!
I have index.html with 1 link and 1 button. Button opens window 1, link opens window 2. I want Window 2 to have a button that can close window 1. Please help if you can!! Thanks!!!
<script type="text/javascript">
function openWin1()
{
myWindow=window.open('','','');
myWindow.document.write("<p><img src=\"flower.jpg\" /></p>");
myWindow.focus();
}
function openWin2()
{
myWindow=window.open('','','');
myWindow.document.write("<style type=\"text/css\">body{background-color:yellow;}</style>
<p><img src=\"bee.jpg\" /></p><input type=\"button\" onclick=\"window.close();\"
value=\"Close Window 1\" />");
myWindow.focus();
}
</script>
</head>
<body>
<input type="button" value="Window 1" onclick="openWin1();" /><br />
<a href="javascript:openWin2();">Window 2</a>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
本地测试(适用于 Chrome,也应该适用于其他浏览器):
Tested this locally (works in Chrome, should work in other browsers too):
从第二个窗口使用
window.opener.mywindow.close()
来首先关闭。为第二个窗口对象选择其他变量名称use
window.opener.mywindow.close()
from second window to close first . Choose other variable name for second window object“window1”和“window2”都可以通过
window.opener
引用原始页面。因此,您的原始页面可以公开一个全局函数,其他页面都可以调用该函数,如下所示:Both "window1" and "window2" can refer to the original page via
window.opener
. Your original page can therefore expose a global function that either of the other pages can call like this: