Chan Chan - 1 year ago 249
Javascript Question

How to detect popup window close event


document.querySelector('#share').onclick = function () {
var popup ='', '', "width=400, height=400");

popup.onbeforeunload = function() {

return false;


<a id="share" href="#">share</a>

Two things:

  1. I want to do something after user finish the facebook share thing, if I share the page by open window the window will be auto close after everything is done, so I want to use
    event to imitate
    after sharing callback
    , but I can't get it when popup is closed.

  2. If user just turn off the window or cancel sharing will also trigger the event, so it's not the best solution, is there any facebook sharing successful callback api?

Answer Source

It does not work due to cross domain calls. To get the unload event you need to call a popup instead, which has the wanted url in an iframe.

document.querySelector('#share').onclick = function(){
    var popup ='popup.html', '', "width=400, height=400");

    popup.onbeforeunload = function(){


<iframe src = ''></iframe>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download