javascript 关闭弹出窗口时如何调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18213521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:05:29  来源:igfitidea点击:

How to call a function when close popup window

javascript

提问by Mobo

I am calling the Javascript window.open()function to load another url in a pop up window. When the users closes the popup window, I want the MyThanks()function to be called. How I can do this?

我正在调用 Javascriptwindow.open()函数以在弹出窗口中加载另一个 url。当用户关闭弹出窗口时,我希望MyThanks()调用该函数。我怎么能做到这一点?

My Script :

我的脚本:

<!DOCTYPE html>
<html>
<head>
    <script>
    function openWin(){
        myWindow=window.open('http://facebook.com/ekwebhost','','width=600,height=400,left=200');
        myWindow.focus();
    }

    function MyThanks(){
        alert("Thanks");
    }
    </script>
</head>
<body>

    <input type="button" value="Open window" onclick="openWin()" />

</body>
</html>


Regarding usage of popups:

关于弹窗的使用:

Are you aware that Facebook & Twitter still use popup windows for user like & follow. Go to http://www.ekwebhost.comand click the Facebook "Like" button or the Twitter "Follow" button. Why can't I use popup windows for my own needs?

您是否知道 Facebook 和 Twitter 仍然使用弹出窗口供用户点赞和关注。转到http://www.ekwebhost.com并单击 Facebook 的“喜欢”按钮或 Twitter 的“关注”按钮。为什么我不能根据自己的需要使用弹出窗口?

回答by Deepak Biswal

function openWin(){
    myWindow=window.open('http://facebook.com/ekwebhost','','width=600,height=400,left=200');
    // Add this event listener; the function will be called when the window closes
    myWindow.onbeforeunload = function(){ alert("Thanks");}; 
    myWindow.focus();
}

Try this!

试试这个!

You can swap out the function(){ alert("Thanks");};for MyThanksto call your function.

您可以换出function(){ alert("Thanks");};forMyThanks来调用您的函数。

回答by RustyTheBoyRobot

The new window that you open will have an attribute called openerthat references the Windowobject that created it. You can then call functions on the parent window by calling window.opener.MyThanks();

您打开的新窗口将有一个称为opener引用Window创建它的对象的属性。然后,您可以通过调用在父窗口上调用函数window.opener.MyThanks();

Popup:

弹出:

<script type="text/javascript">
  function closePopup() {
    window.opener.MyThanks();
    window.close();
  }
</script>
<div>
  <h1>My Popup</h1>
  <!-- Form or info to display -->
  <button onclick="closePopup();">Close</button>
</div>

While there is nothing inherently wrongwith browser popup windows, they can be annoying since the user can lose them behind other windows. Also, it takes their focus off of your page to do something else. The application that I currently develop is trying to move all of our popup windows to Javascript dialogs. It gets pretty annoying for users once you get 3 popups deep.

虽然浏览器弹出窗口本身没有任何问题,但它们可能很烦人,因为用户可能会在其他窗口后面丢失它们。此外,它会将他们的注意力从您的页面上转移到做其他事情上。我目前开发的应用程序正在尝试将我们所有的弹出窗口移动到 Javascript 对话框。一旦您获得 3 个弹出窗口,这对用户来说会变得非常烦人。

There are a bunch of ways to create dialogs within your webpage. jQuery UIis one library that I like.

有很多方法可以在网页中创建对话框。jQuery UI是我喜欢的一个库。

回答by Arijit Jana

You can try this:

你可以试试这个:

  <script>
    function openWin(){
        myWindow=window.open('http://facebook.com/ekwebhost','','width=600,height=400,left=200');
        myWindow.focus();
       MyThanks();

    }
     function MyThanks(){
        alert("Thanks");
    }


    </script>