在onbeforeunload中使用Ajax自动保存字段数据

发布于 2024-12-23 18:32:50 字数 1022 浏览 7 评论 0原文

我正在尝试在 window.onbeforeunload 函数中使用 AJAX 来自动保存我网站页面上的文本字段(没有保存按钮,这一切都是在后台完成的)。保存功能是用PHP编写的。我首先尝试以下操作:

window.onbeforeunload = function(e){
  var noteelement = document.getElementById( 'note' );
  var mynote = noteelement.value;

  $.post('save_my_note.php', { thenote: mynote }, function(data){});
  };

在研究中,我逐渐了解到,由于 ajax 调用的异步性质,不会调用 php 代码,因为窗口在函数从服务器触发之前关闭。

当然,正如其他人所说,如果我向 onbeforeunload 函数添加一个返回值以导致弹出对话框(这将是“您确定要离开吗”框),那么它确实可以工作,因为 .post 调用在对话框等待响应时有时间运行:

  $.post('save_my_note.php', { thenote: mynote }, function(data){});
  return mynote;
  };

据说我必须将此 post 调用更改为异步 AJAX,以便该函数等待函数完成,但不太熟悉语法,我不太确定我需要的语法。我尝试了以下方法来代替 post 函数:

        $.ajax({
            url: "save_my_note.php",
            data: "{thenote: mynote}",
            async: false                 
        });

但是,这似乎也不起作用。

有谁有一个简单的代码示例,用于通过 onbeforeunload (或 onunload??) 函数以同步方式实现自动保存?我不是 javascript 或 jquery 专家,而且我在网上找到的示例令人困惑。

I am trying to use AJAX in the window.onbeforeunload function to autosave a text field that is on a page in my site (there is no save button, it is all done in the background). The saving function is written in PHP. I started by trying the following:

window.onbeforeunload = function(e){
  var noteelement = document.getElementById( 'note' );
  var mynote = noteelement.value;

  $.post('save_my_note.php', { thenote: mynote }, function(data){});
  };

In researching, I have come to understand that due to the asynchronous nature of the ajax call, the php code is not called because the window closes before the function fires off from the server.

Of course, as others have said, if I add a return value to onbeforeunload function to cause the dialog box to pop up (that would be the "are you sure you want to leave" box), it DOES work since the .post call has the time to get run while the dialog box waits for a response:

  $.post('save_my_note.php', { thenote: mynote }, function(data){});
  return mynote;
  };

It has been said that I have to change this post call to asynchronous AJAX so that the function waits for the function to complete, but not being very versed in the syntax, I am not quite sure of the syntax I need. I tried the following in place of the post function:

        $.ajax({
            url: "save_my_note.php",
            data: "{thenote: mynote}",
            async: false                 
        });

However, that just doesn't seem to work either.

Does anyone have a simple code example for implementing the autosave in a synchronous manner via the onbeforeunload (or the onunload??) function? I'm not a javascript or jquery expert, and the examples I have found online have been confusing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风柔一江水 2024-12-30 18:32:50

您的最后一个示例应该可以工作,您需要将 asynch: false 更改为 async: false (必须删除额外的 h根据 jQuery 文档

Your last example should work, you need to change asynch: false to async: false (the extra h must be removed) according to jQuery documentation

近箐 2024-12-30 18:32:50

也许你可以强制超时暂停 - 不知道这是否真的有效,只是猜测:

var bSaved = false;
window.onbeforeunload = function(e){
  bSaved = false;
  var noteelement = document.getElementById( 'note' );
  var mynote = noteelement.value;

  $.post('save_my_note.php', { thenote: mynote }, function(data){ bSaved = true; });

  window.setTimeout('bSaved = true',2000);

  while (!bSaved) {}

  };

Maybe you could force a timeout pause - dunno if this'll actually work, just a guess:

var bSaved = false;
window.onbeforeunload = function(e){
  bSaved = false;
  var noteelement = document.getElementById( 'note' );
  var mynote = noteelement.value;

  $.post('save_my_note.php', { thenote: mynote }, function(data){ bSaved = true; });

  window.setTimeout('bSaved = true',2000);

  while (!bSaved) {}

  };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文