JavaScript How to call a function when close popup window call function when state changes react,call function when input

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?

Answer: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:

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

Answer:2
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.

Answer:3

Let's say I have an HTML page, served up from example.com. It makes an javascript ajax request to targetServer.com Which server needs to return a Access-Control-Allow-Origin="(something)"? Is it the ...

Let's say I have an HTML page, served up from example.com. It makes an javascript ajax request to targetServer.com Which server needs to return a Access-Control-Allow-Origin="(something)"? Is it the ...

  1. server that needs staff
  2. who needs server
  3. server that needs help

Hi I have this URL (top) I'm trying to use with "pinterest" they change the url (bottom). Does anybody know how I can change my (top) url to be the same as the "pinterest" url. Is there a Jquery ...

Hi I have this URL (top) I'm trying to use with "pinterest" they change the url (bottom). Does anybody know how I can change my (top) url to be the same as the "pinterest" url. Is there a Jquery ...

I'm trying to get JSON response from server using restangular. var baseAccounts = Restangular.one('getAllCustomers'); baseAccounts.getList().then(function(customers) { $scope.myData = ...

I'm trying to get JSON response from server using restangular. var baseAccounts = Restangular.one('getAllCustomers'); baseAccounts.getList().then(function(customers) { $scope.myData = ...

Can someone explain the following code? inputWords is supposed to be an array containing various words and this function is supposed to return an array containing the number of times a word appears in ...

Can someone explain the following code? inputWords is supposed to be an array containing various words and this function is supposed to return an array containing the number of times a word appears in ...

  1. javascript reduce function example
  2. javascript reduce function explained
  3. javascript reduce function array of objects
  4. javascript reduce function on object
  5. javascript reduce function mdn
  6. javascript reduce function implementation
  7. javascript reduce function
  8. javascript reduce function parameters
  9. javascript reduce function array
  10. javascript reduce function syntax
  11. javascript reduce function callback
  12. javascript reduce function calls
  13. javascript reduce arrow function
  14. javascript array reduce function example
  15. javascript reduce async function
  16. javascript map reduce function
  17. javascript reduce anonymous function
  18. javascript es6 reduce function
  19. javascript return reduce function
  20. javascript reduce is not a function