开发者

When Does the File Download Dialog Open? - HTML, Javascript

开发者 https://www.devze.com 2022-12-25 05:04 出处:网络
I have a html page, and when I click a file link on the page, the file download dialog pops up, and this file dialog locks the page. I mean without selecting one of the choices(Open, Save, Cancel) I c

I have a html page, and when I click a file link on the page, the file download dialog pops up, and this file dialog locks the page. I mean without selecting one of the choices(Open, Save, Cancel) I can't do anything on the page (which is normal). What I need is if javascript can check whether the page开发者_C百科 is locked or not. (or whether the file dialog is popped?)

p.s. don't say "put an onclick event to the link", because the server may respond very slowly (like 30 seconds after clicking)


If you know that your server is returning a file for download, then you can guess when the dialog comes up by this round-about trick (which I got from a stackoverflow answer to a related question I asked):

  1. When you issue the HTTP request from your page, add a parameter whose value is some random string (like "random" + new Date().getTime() — it doesn't have to be secure but you want to avoid collisions)

  2. The server looks for that parameter. When it starts sending back the attachment for download, it adds a cookie with your random string as its value.

  3. Now, after the page issues the HTTP request, it starts an interval timer. The code in the timer function checks document.cookie looking for that random string. As soon as document.cookie contains that string, then you know the HTTP response has made it to the browser. Since you know that the browser will ask the user to save the attachment, you can infer that the file dialog is being shown at that point.

What I needed this for was a way to deal with the IE security thing about file attachments coming from events other than direct user "clicks". I needed to close a modal dialog, so I needed to know when the HTTP response would return.


Because the page is locked, you can't do anything with JavaScript, because it is locked as well.

But, what are you trying to do? Are you trying to somehow log the fact that the user is downloading the file? If yes, there are better ways to do it, and they're on the server-side. Use some server-side scripting language to serve the file and log the fact that it was downloaded.

If that's not what you're trying to do, then the only way is using either onclick on the link or onunload/onbeforeunload, but these are less reliable and I am sure that you will find completely different behavior on different browsers.

Actually, now that I think of it, there is one more way, but it's very dirty. The idea is to set an interval to run every second and check if between two runs more than a second has passed. Something like:

var lastTime = new Date().getTime();
function checkTime() {
    var curTime = new Date().getTime();
    if (curTime - lastTime > 1100) { // 1100 because there might be small browser lags 
        // do something after the dialog appeared and the user did something with it
    }
    lastTime = curTime;
}
setInterval(checkTime, 1000);

Please note, that there are browsers (Chrome is an example, I think) that do not block the page while that dialog is opened, so this might not work. Make sure to double-cross-check everything if you go about using this.

I have to go take a shower now.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号