简体   繁体   中英

window.onbeforeunload submit form

I'm working with Facebook's sharing feature and currently it's setup to use on a private computer. Meaning, once the person shares something, they will still be logged in.

The problem I'm having is that once the user clicks share, it closes the window right after.

I was hoping that I would have some luck using window.onbeforeunload . I'm trying to run this code, before the window closes. And I thought doing something like this would allow it to work. Not having any luck though.

 window.onbeforeunload = function() {
  $('#logout_form').submit();

}

The only thing you can reliably do during an onbeforeonload handler is decide whether to pop up a confirmation dialog. You cannot trigger new actions, such as submitting a form, from such a handler.

duskwuff is correct that you can't submit a form. but there are somethings you can do. I've used Atmosphere and it makes an ajax call on the unload.

jQuery(window).unload(function() {
    jQuery.atmosphere.unsubscribe();
});

which makes an request call. You should be able to make an ajax post to the server.

I found a similar example: Ajax post onbeforeunload doesn't work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM