Hello!

Tell me, how can I call the js-function in a document located in the iframe when the parent window is closed? $(window)on('unload', function(){}) does not respond. Reacts only, for example, when the frame is rebooted (by pressing the corresponding item in the iframe context menu)

The bottom line is that the application is in the iframe and I would like to make a disconnect () to close the page;

    3 answers 3

    main.html:

     <html> <body id="B1"> <iframe src="/f1.html"></iframe> <a href="/">go</a> </body> 

    f1.html:

     <html> <head> <script src="jquery.min.js"></script> </head> <body> <h1>This is frame</h1> <script> $( window.parent ).unload(function() { alert("parent window unloading"); }); </script> </body> 

    Works in FF. For Chrome (and for FF) the correct version of the script from f1.html is:

     <script> $(window.parent).on('beforeunload', function(){ alert("parent window unloading"); }); </script> 
    • Unfortunately, it does not work. I tried in chrome 60.0.3112.113. I use jquery-3.2.1 - Vasily Koshelev
    • I had a hand 2.x. And it works when refreshing and leaving the page by reference. If you just close the browser window, then yes - it does not work - Boris
    • In general, instead of the alert, I had the function call diconnect () ;. And it did not work, but if you put not the function call, but the function code, then everything worked. Your answer was the most correct, thanks! - Vasily Koshelev
     $('iframe').ready(function(){ alert('Работает?'); }) 

    You can also try

     $('iframe').load(function(){ alert('2'); }); 
    • What is the difference between these fragments? - 0xdb