Hi Experts,

I am using mozilla firefox 1.5. I want to delay the page close when the user clicks on the close (X) button of the browser. I want to display something like "Closing Please wait......" for 5 seconds (run a code in the background) and then close the window after 5 seconds.

Till the time the "Closing Please wait......" is displayed, the window should not close. after 5 seconds, it will as it is close because the user has clicked the browser's close button.

Is there a way I can do this ?

Regards

/nbi

Recommended Answers

All 2 Replies

Why on earth would you want to do something so outright annoying? If I click close, I want closed.

No, there isn't an easy away to do this. You can catch the body "onunload" javascript event, and run code there; but it doesn't mean the same thing in different browsers. Regardless, you cannot 'cancel' or 'delay' the closing of the page or browser.*

Imagine the user accessing a page as them opening and closing their browser simultaneuously; that's effectively how HTTP works. You can't make any assumptions about what the user ( or browser ) does once they have downloaded your page, and certainly don't view them as being 'on' your page; they're just looking at a copy of it.


EDIT: * just to add, it's not possible in most ( all modern ? ) browsers, for reasonable security reasons, not because it would be technically difficult.

Well. it seems firefox truely sucks, it allows this:

<html>
<head></head>
<body onunload="while( true ){ alert( 'hi' ); }">
</body>
</html>

which executes when the user closes the browser, or navigates away from the page. Opera doesn't, because Opera's a good browser, and even if it did, one could break out of it quickly.

That only works to 'delay' the browser closing because alert is a blocking function; it doesn't return until the user clicks ok. if you could find another blocking function that isn't so insidious, you could run that.. or even 'while' loop repeatadly. It'll only work for firefox ( and likely IE to be honest ), and it'll likely annoy your users... but hey.. also, the browser window closes fully in FF2, and an alert just pops up until the program's terminated manually... so, you can't 'see' the page behind atall, but you can execute any code you desire to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.