3

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?

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.com and click the Facebook "Like" button or the Twitter "Follow" button. Why can't I use popup windows for my own needs?

RustyTheBoyRobot
  • 5,891
  • 4
  • 36
  • 55
Mobo
  • 118
  • 1
  • 1
  • 7
  • 1
    Look for `opener`. http://stackoverflow.com/questions/10591050/javascript-window-opener-call-parent-function – emerson.marini Aug 13 '13 at 15:43
  • 5
    Ugh. Pop-up windows, then pop-ups that appear when you close the pop-ups. Are you stuck in 1995? We did away with all this rubbish two decades ago. – Lightness Races in Orbit Aug 13 '13 at 15:47
  • 2
    Are you aware that Facebook & Twitter still use popup windows for user like & follow. Go to http://www.ekwebhost.com and click the Facebook "Like" button or the Twitter "Follow" button. Why can't I use popup windows for my own needs? – Mobo Aug 13 '13 at 17:27
  • opening up another domain makes you lose the ability to know what is happening in the pop up. If you want someone to like something, you should probably use the facebook apis – epascarello Jun 01 '20 at 22:16
  • Does this answer your question? [How to call parent window function on close of child window in javascript?](https://stackoverflow.com/questions/22775700/how-to-call-parent-window-function-on-close-of-child-window-in-javascript) – zcoop98 Oct 08 '21 at 21:49

3 Answers3

1
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 MyThanks to call your function.

RustyTheBoyRobot
  • 5,891
  • 4
  • 36
  • 55
Deepak Biswal
  • 4,280
  • 2
  • 20
  • 37
  • Note `myWindow` must be the same variable which you have declared for `window.open` – Deepak Biswal Aug 13 '13 at 15:47
  • Works fine for me. This is a better answer than mine, especially if you have no control over the popup content. – RustyTheBoyRobot Aug 13 '13 at 17:13
  • I do what you say. Go http://alif.webatu.com/ you will see my work. This work before full loaded page. When popup content full loaded it's can't work. So what you suggestion now? – Mobo Aug 14 '13 at 02:06
0

The new window that you open will have an attribute called opener that references the Window object that created it. You can then call functions on the parent window by calling 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. It gets pretty annoying for users once you get 3 popups deep.

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

RustyTheBoyRobot
  • 5,891
  • 4
  • 36
  • 55
  • I have not any control on popup window. So what I can do now? – Mobo Aug 13 '13 at 16:55
  • You should add that constraint to your question. If you don't have control over the popup window, how does it close? Does the URL that you load have Javascript to call `window.close()`? – RustyTheBoyRobot Aug 13 '13 at 16:58
  • Go http://alif.webatu.com/ and click open window. This will open a facbook fan page in popup window. When page full loaded click on close button (x). I want when you click close button then run MyThanks() function, which I wrote on my script. – Mobo Aug 13 '13 at 17:19
  • It didn't work because that page overrides the window events. You will have to step through the javascript on that page and figure out how to register your `onbeforeunload` with their event handlers. – RustyTheBoyRobot Aug 14 '13 at 13:31
-1

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>
zcoop98
  • 2,590
  • 1
  • 18
  • 31
Arijit Jana
  • 220
  • 1
  • 4