views:

316

answers:

2

Hello, 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 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)

+1  A: 

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.

Felix
that dirty solution is so smart :). thank you.
sahs
Unfortunately the solution didn't work in IE6. That was smart though :).
sahs
I might be that it just needs a bit of tweaking to work on IE6 (I don't see why it wouldn't, unless IE6 doesn't block the page). Maybe IE6 doesn't know about `.getTime()` or something. Does it give any errors?
Felix
Actually, IE6 does block the page, but javascript still runs. That's the reason it didn't work.
sahs
+1  A: 
Pointy
That is worth a try, I'll inform you about the result after the try. Thanks.
sahs