简体   繁体   English

window.onbeforeunload提交表单

[英]window.onbeforeunload submit form

I'm working with Facebook's sharing feature and currently it's setup to use on a private computer. 我正在使用Facebook的共享功能,目前已将其设置为在私人计算机上使用。 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 . 我希望使用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. onbeforeonload处理程序期间,您唯一可以可靠地执行的操作就是确定是否弹出确认对话框。 You cannot trigger new actions, such as submitting a form, from such a handler. 您无法从此类处理程序触发新操作,例如提交表单。

duskwuff is correct that you can't submit a form. duskwuff是正确的,您不能提交表格。 but there are somethings you can do. 但是您可以做一些事情。 I've used Atmosphere and it makes an ajax call on the unload. 我用过Atmosphere,它在卸载时进行了ajax调用。

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

which makes an request call. 发出请求呼叫。 You should be able to make an ajax post to the server. 您应该能够向服务器发布ajax发布。

I found a similar example: Ajax post onbeforeunload doesn't work 我发现了一个类似的示例: Ajax发布onbeforeunload不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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