从任何地方关闭javascript-popup-window [英] Close javascript-popup-window from anywhere

查看:89
本文介绍了从任何地方关闭javascript-popup-window的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个可以从任何地方关闭的弹出窗口。

I'm trying to build a popup that can be closed from anywhere.

在主页面上,您可以选择打开它。在浏览主页的任何时候,用户都可以再次关闭它。

On the Mainpage you have the option to open it. At any point while browsing the mainpage the user shall be able to close it again.

我找到了这个基本的弹出控件:

I found this basic pop-control:

 function popuponclick()
   {
      my_window = window.open("", "mywindow","status=1,width=350,height=150");
   }

   function closepopup()
   {
      my_window.close ();
   }

问题,一旦你离开打开弹出窗口的页面,该函数失去了它的连接,弹出窗口将不再关闭。

The problem, as soon you leave the page from where you opened the popup, the function looses its connection and the popup won't close anymore.

有没有办法在全局范围内关闭和关闭这个特定的弹出窗口?

Is there a way to adress and close this certain popup pagewide?

推荐答案

根据实际问题,我提出了三种解决方案。

There are three solutions I propose depending on what the problem actually is.

如果您需要在从页面A打开它时关闭页面B中的弹出窗口,则以下代码将允许您获取对来自页面B的弹出窗口并关闭它。
参考:查找以前由window.open打开的窗口

If you need to close the popup from page B when it was opened from page A the following code will allow you to obtain a reference to the popup from page B and close it. Reference: Find window previously opened by window.open

PageA.html

PageA.html

<script>
 function popuponclick()
   {
      my_window = window.open("http://google.com", "mywindow","status=1,width=350,height=150");
   }

   function closepopup()
   {
      my_window.close ();
   }
</script>

<a href="javascript:popuponclick()">Open window...</a></br>
<a href="PageB.html">Go to Page B</a>

</body>
</html>

PageB.html

PageB.html

<html>
<body>

<script>
   function closepopup()
   {
      var popupPlayer= window.open('', 'mywindow', 'status=1,width=350,height=150') ;
      popupPlayer.focus();
      popupPlayer.close();
   }
</script>

<a href="javascript:closepopup()">Close window...</a></br>

</body>
</html>



2)closepopup()在打开窗口的同一页面上不起作用



需要对my_window进行全局引用,因此您需要更改以下代码:

2) closepopup() does not work on the same page as window is opened

A global reference to my_window is needed so you would need to change the code like this:

     var my_window; //global reference here
    function popuponclick()
       {
          my_window = window.open("", "mywindow","status=1,width=350,height=150");
       }

       function closepopup()
       {
          my_window.close ();
       }



3)使用框架的最终第三解决方案



您将页面分成1帧(一个是100%),另一个是0%。

3) Final third solution using frames

You separate your page into 1 frames (one which is 100%) and another that is 0%.

您添加窗口打开/关闭代码在父框架中。
比通过跨框架javascript命令调用窗口控制代码。

You add the window open/close code in the parent frame. Than you call the window control code via cross frame javascript commands.

框架包含Javascript的HTML

Frames HTML Containing Javascript

<html>
<head>
<title>My example</title>

<script>

  var my_window;

 function popuponclick()
   {
      my_window = window.open("", "mywindow","status=1,width=350,height=150");
   }

   function closepopup()
   {
      my_window.close ();
   }
</script>

</head>
<frameset cols="100%,0%">
<frame src="frame.html"/>
<frame/>
</frameset>
</html>

内部框架(frame.html) - 调用父Javascript

Internal Frame (frame.html) - calling parent Javascript

<html>
<body>
<a href="javascript:parent.popuponclick()"> Open Window </a><br/>
<a href="javascript:parent.closepopup()"> Close Window </a><br/>
<a href="javascript:location.href=location.href"> Refresh Frame (and try again)</a><br/>
</body>
</html>

这篇关于从任何地方关闭javascript-popup-window的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆