简体   繁体   English

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

[英]How to call a function when close popup window

I am calling the Javascript window.open() function to load another url in a pop up window.我正在调用 Javascript window.open()函数以在弹出窗口中加载另一个 url。 When the users closes the popup window, I want the MyThanks() function to be called.当用户关闭弹出窗口时,我希望MyThanks()函数。 How I can do this?我怎么能做到这一点?

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.您是否知道 Facebook 和 Twitter 仍然使用弹出窗口供用户喜欢和关注。 Go to http://www.ekwebhost.com and click the Facebook "Like" button or the Twitter "Follow" button.访问http://www.ekwebhost.com并单击 Facebook 的“赞”按钮或 Twitter 的“关注”按钮。 Why can't I use popup windows for my own needs?为什么我不能根据自己的需要使用弹出窗口?

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");};你可以换掉function(){ alert("Thanks");}; for MyThanks to call your function.MyThanks调用您的函数。

The new window that you open will have an attribute called opener that references the Window object that created it.您打开的新窗口将具有一个名为opener的属性,该属性引用创建它的Window对象。 You can then call functions on the parent window by calling window.opener.MyThanks();然后,您可以通过调用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 wrong with 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.我目前开发的应用程序正在尝试将我们所有的弹出窗口移动到 Javascript 对话框中。 It gets pretty annoying for users once you get 3 popups deep.一旦深入 3 个弹出窗口,用户就会非常烦人。

There are a bunch of ways to create dialogs within your webpage.有很多方法可以在您的网页中创建对话框。 jQuery UI is one library that I like. jQuery UI是我喜欢的一个库。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM