duanjuebin2519 2013-05-23 21:38
浏览 68
已采纳

如何结合内容处置:附件暂时停用onbeforeunload-message?

I have one page insert.php where the user can make inputs into a form.

I use javascript to inform the user about possible loss of inserted data when the page is closed.

<script language="JavaScript">
    var needToConfirm = true;

    window.onbeforeunload = confirmExit;

    function confirmExit() {
        if ( needToConfirm )
        return "This page is asking you to confirm that you want to leave - data you have entered may not be saved.";
    }
</script>

Then I have a form that contains an element to download ("save") the data inserted.

<form action='insert.php' method='post'>
    ... other input elemets ...
    <input type="submit" name="save" value="Save" onclick="needToConfirm = false;">
</form>

When this save-button is pressed, no data is lost, since the form just reloads itself and refills the form with the $_POST-data. Hence I set the needToConfirm variable to false to not make the user worried.

Furthermore, the $_POST-data is downloaded when reloading the page (insert.php) using:

session_start();
... store $_POST in $_SESSION ...
Header( 'HTTP/1.1 301 Moved Permanently');
Header($'Location: save.php');

Where in save.php I do this:

session_start();
header( 'Content-disposition: attachment; filename=download.dat' );
... echo $_SESSION-data ...

This downloads the inserted data without closing the already loaded insert.php.

The problem is that the javascript needToConfirm-variable stays on false. Hence when I close the page after clicking on download (even if the download was not confirmed by the user) no warning is shown anymore and data may be lost.

Any ideas how to solve this problem are very appreciated.

(I guess the problem is related to the insert.php somehow not fully reloaded due to the loading of the other page...)

  • 写回答

1条回答 默认 最新

  • dqb77047 2013-05-23 21:51
    关注

    you would need to set up a timer and after some timeout, reset the value again to true (you should not have to use bigger then 100ms)

    window.setTimeout('needToConfirm=true', 100);
    

    Put that into the onclick so when there is content to show, the page reloads itself, but when there is just the attachment, the timer fires

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配