|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
How to stop browser from closing using Javascript?
In my application, i need to do some checking when user close the browser.if the checking returns false, then the window should not be closed even user press 'X' to close; else then let the window closed. Is it related to onUnload event? but i don't know how to prevent the winodw from closing. Please Help. THanks a lot.
|
|
#2
|
|||
|
|||
|
Hi,
I also looking for same solution. If you have found, could you please forward me that solution. to my email: m_subbareddy@hotmail.com I also need to do some checking when user close the browser.if the checking returns false, then the window should not be closed even user press 'X' to close; else then let the window closed. So, forthat I want to implement in the following.. <script language='javascript'> function onFeforeUnloadAction(){ var flag=window.confirm("You are closing the window. do you want to continue. Click 'Ok' to close or click 'Cancel' to stay back"); if(flag) { alert("bye"); } else { // Stop the closing browser //.... } } window.onbeforeunload = function(){if((window.event.clientX<0) || (window.event.clientY<0)){ onBeforeUnloadAction();} </script> But, same can be handle by setting the event.returnvalue="mesg". But, how to get to know whether user clicked "Ok" or "Cancel" if we implement following way. <script language='javascript'> function onFeforeUnloadAction(){ var flag var mesg ="You are closing the window. do you want to continue. Click 'Ok' to close or click 'Cancel' to stay back"; return mesg; } window.onbeforeunload = function(){if((window.event.clientX<0) || (window.event.clientY<0)){ return onBeforeUnloadAction();} </script> Thanks in advance, ~ SubbaReddy |
|
#3
|
|||
|
|||
|
Never messed around with that, but I know something similar was discussed here:
http://haneng.com/Forums_Post.asp?id=3542&r=2 Perhaps you can use that information to make it work, I think they got a working solution at the end of the thread. |
|
#4
|
|||
|
|||
|
//this was taken from the microsoft website
//call this function in your onbeforeunload event code function checkClose() { event.returnValue = "If you are currently in a lesson and you close this screen your data will not be captured and you will not receive credit."; } |
|
#5
|
|||
|
|||
|
Hello,
Did you find the solution for the problem. I have to do the same. Thanks, Subhash Quote:
|
|
#6
|
|||
|
|||
|
solution for the above problem is as follows...
var needToConfirm = false;
function setDirtyFlag() { needToConfirm = true; //Call this function if some changes is made to the web page and requires an alert // Of-course you could call this is Keypress event of a text box or so... } function releaseDirtyFlag() { needToConfirm = false; //Call this function if dosent requires an alert. //this could be called when save button is clicked } window.onbeforeunload = confirmExit; function confirmExit() { if (needToConfirm) return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?"; } Last edited by KKarthikeyan : April 7th, 2006 at 10:25 AM. Reason: need a change in subject |
|
#7
|
||||
|
||||
|
window closing
You can not use javascript to prevent users from closing the window any more than you can force users to allow pop-ups.
I suppose you could script some obnoxious iterating code, but that will surely PO your users. If you wanted to have a "confirm" warning, you could do so with something like this, (but with confirm instead of alert, of course). ..... function winUnload(){ alert("Unload Window"); } window.onunload = function(){ winUnload(); } ..... Last edited by Mittineague : April 7th, 2006 at 04:00 PM. |
|
#8
|
||||
|
||||
|
My usual response:
The question is not how but why?? If a user wants to close his browser, that's his god-given right! If you don't want him to close it, make it so there's something interesting in the window. (Naked women seem to do the trick for a lot of people)
__________________
This is my code. Is it not nifty? "The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools." ---Douglas Adams Join the Itsacon fanclub! Zero Tolerance: Spammers banned so far: 275
![]() |
|
#9
|
||||
|
||||
|
This thread scares me.
__________________
Daryl's Homepage | My Blogroll | My Profile | Firefox supporter! DevArticles Forum Moderator "The net is a waste of time, and that's exactly what's right about it." -- William Gibson |
|
#10
|
||||
|
||||
|
madcowdzz, i couldnt agree more, lol
you could try and use a onunload="ul()" function ul() { if ([its not ready to close]) {window.open(parent.location);} } it will reopen itself and if you wanted to get to the same position, save stuff in cookies as they click or as they select stuff therefore onload of the new window, it will re-load all the information they selected colton22 |
|
#11
|
||||
|
||||
|
no close
There is a good reason to confirm a window close. If I filled out a bunch of form inputs, I would appreciate a "last minute warning" before the info was lost. But, if this is done in an annoying way, keep in mind that javascript can't prevent a user from shutting down his computer. And you can bet they'll NEVER come back.
|
|
#12
|
||||
|
||||
|
Quote:
You've picked a great scenario, however this described behaviour would be better implemented by an internet browser itself, not your web page. |
|
#13
|
|||
|
|||
|
To warn the user before exiting the page, all you need to do is:
<script type="text/javascript"> window.onbeforeunload = function(){ if(condition){ return "Message you want to display to user" } } </script> The problem that I have at the moment is how to catch the cancel event if the user presses cancel in the onbeforeunload dialog that appears. Any help would eb much appreciated. |
|
#14
|
||||
|
||||
|
Don't throw the message over a return statement, throw it out using in alert() call.
Then have the function return true if the close is acknowledged, or false if not. IIRC, most browsers will cancel an event if the eventHandler returns false. |
|
#15
|
|||
|
|||
|
onbeforeunload
That's the problem with the onbeforeunload event, you have to return a string, true or false will not work.
And I need to to use the onbeforeunload event as I want to catch if a user closes the browser. Any ideas? Thanks. |