【问题标题】:Is it possible to catch browser's File Open/Save dialog event using Javascript是否可以使用 Javascript 捕获浏览器的文件打开/保存对话框事件
【发布时间】:2021-06-29 11:02:49
【问题描述】:

使用 Javascript 可以监听浏览器的文件打开/保存对话框事件。当我收到保存文件对话框现已打开的通知时,我想执行一项操作。具体来说,我想在对话框打开时隐藏加载微调器(但这很可能是任何其他操作)

我相信我可以为我创建的对话框做到这一点,但不确定是否可以为浏览器的标准对话框做到这一点。

对此的任何指示都会非常有帮助。

【问题讨论】:

标签: javascript jquery browser dom-events


【解决方案1】:

是的!您可以利用大多数浏览器(在 Chrome、Firefox 和 IE 上测试正常)在单个文件下载对话框打开之前触发 beforeunload 事件。

所以这样的代码可以工作:

$(window).bind ("beforeunload",  function (zEvent) {
    // PERFORM DESIRED ACTIONS HERE.
    /* This code will fire just before the Individual-file Download 
       dialog opens.
       Note that it will also fire before the tab or window is closed, 
       but that should not be a problem for this application.
    */
} );


打开并运行这个 sn-p 以查看它的实际效果:

$(window).bind ("beforeunload",  function (zEvent) {
    $("#dwnldStatus").text ("This code runs just before the file open/save dialog pops up.");
} );

$("#directDwnload").click ( function () {
    fireDownload ();
} );

$("#ResetTimer").click ( function () {
    $("#dwnldStatus").html (
        'Download will start in <span id="timeleft">3</span> seconds.'
    );
    fireTimer (3);
} );

function fireDownload () {
    window.location.assign (
        "//phs.googlecode.com/files/Download%20File%20Test.zip"
    );
}

function fireTimer (secondsLeft) {
    this.secondsLeft    = secondsLeft || 30;
    this.countdownTimer = this.countdownTimer || null;

    if ( ! this.countdownTimer) {
        this.countdownTimer = setInterval ( function() {
                this.secondsLeft--;
                $("#timeleft").text (this.secondsLeft);
                if (this.secondsLeft <= 0) {
                    clearInterval (this.countdownTimer);
                    this.countdownTimer = null;
                    fireDownload ();
                }
            },
            1000
        );
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<p>Activate one of the download buttons.  The timer button is just like any other javascript initiated download, no additional  click is needed.</p>
<p>The javascript detects when the File/Save dialog pops up and changes the status to "This code runs just before the file open/save dialog pops up.".</p>
<p>Note that it is not necessary to download the file. You can cancel the download.</p>

<div id="dwnldStatus"></div>
<button id="ResetTimer">Set timer to 3 seconds.</button>
<button id="directDwnload">Download the file now.</button>

请注意,beforeunload 也会在选项卡或窗口关闭之前触发,因此请做好相应计划。如上所述,这不应该是这个问题的问题。

【讨论】:

  • 这可以根据需要工作。不知道为什么这个答案的评价不高。
【解决方案2】:

不,没有事件。

【讨论】:

  • 为什么要失望?我会说这是保护用户隐私的一件非常好的事情。顺便说一下 +1。
  • @BrockAdams,这确实遇到了一个有用的场景。但它只有在他们导航到一个 zip 时才有效,而不是当他们按下 Ctrl-O、Ctrl-S、右键单击将图像另存为等时。
  • @MatthewFlaschen,问题似乎是针对页面/应用程序准备下载单个文件的场景。 Ctrl-O 和 Ctrl-S 不适用。右键单击也不适用于最常见的应用程序。
【解决方案3】:

观察所有可以调用文件对话框的元素。

例如,处理&lt;input type="file" /&gt; 元素上的点击事件。 类似:

$('input:file').click(function(){ // onclick of file input
    $('.spinner').hide(); // hide spinner
});

【讨论】:

  • 我认为他在谈论常规的文件->保存,文件->打开对话框,您无法观看例如浏览器的工具栏或菜单栏。
  • 我说的是当您下载带有内容处置作为附件的文件时出现的对话框。我想你不能看那个事件。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-23
  • 1970-01-01
相关资源
最近更新 更多