wrkbrs

현재창을 닫고 다른창의 함수를 호출하기. 본문

JS

현재창을 닫고 다른창의 함수를 호출하기.

zcarc 2019. 1. 20. 03:07

How to call a function when close popup window



1

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?

0

The new window that you open will have an attribute called opener that references the Windowobject 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.

0
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.



https://stackoverflow.com/questions/18213521/how-to-call-a-function-when-close-popup-window