Skip to content Skip to sidebar Skip to footer

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. When the users closes the popup window, I want the MyThanks() function to be called. How

Solution 1:

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:

<scripttype="text/javascript">functionclosePopup() {
    window.opener.MyThanks();
    window.close();
  }
</script><div><h1>My Popup</h1><!-- Form or info to display --><buttononclick="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.

Solution 2:

functionopenWin(){
    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.

Solution 3:

You can try this:

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

  }

  functionMyThanks() {
    alert("Thanks");
  }
</script>

Post a Comment for "How To Call A Function When Close Popup Window"