JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 30th, 2004, 05:05 AM
Hanna Hanna is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 1 Hanna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old June 24th, 2004, 06:04 AM
msubbareddy msubbareddy is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 1 msubbareddy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question How to stop the window on closing using javascript?

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

Reply With Quote
  #3  
Old June 24th, 2004, 09:16 AM
EiSa EiSa is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Location: Norway
Posts: 184 EiSa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 24 sec
Reputation Power: 13
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.

Reply With Quote
  #4  
Old November 9th, 2004, 02:53 PM
bcaudill bcaudill is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 1 bcaudill User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Talking alert and confirm close

//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.";
}

Reply With Quote
  #5  
Old February 22nd, 2005, 08:05 AM
msubhash msubhash is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 1 msubhash User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 6 sec
Reputation Power: 0
Hello,

Did you find the solution for the problem. I have to do the same.

Thanks,
Subhash

Quote:
Originally Posted by msubbareddy
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

Reply With Quote
  #6  
Old April 7th, 2006, 11:24 AM
KKarthikeyan KKarthikeyan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 1 KKarthikeyan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 7 sec
Reputation Power: 0
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 11:25 AM. Reason: need a change in subject

Reply With Quote
  #7  
Old April 7th, 2006, 04:57 PM
Mittineague's Avatar
Mittineague Mittineague is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jul 2005
Location: West Springfield, Massachusetts
Posts: 552 Mittineague User rank is Private First Class (20 - 50 Reputation Level)Mittineague User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 9 h 1 m 33 sec
Reputation Power: 10
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 05:00 PM.

Reply With Quote
  #8  
Old April 8th, 2006, 01:19 PM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,030 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 39 m 41 sec
Reputation Power: 12
Send a message via ICQ to Itsacon
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: 592

Reply With Quote
  #9  
Old April 10th, 2006, 12:28 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 14
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

Reply With Quote
  #10  
Old July 7th, 2006, 04:37 PM
colton22's Avatar
colton22 colton22 is offline
\ ^_^ / - *Local Friend*
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2006
Location: near chicago, Illinois
Posts: 469 colton22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 1 h 36 m 37 sec
Reputation Power: 9
Send a message via AIM to colton22 Send a message via MSN to colton22 Send a message via Yahoo to colton22
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

Reply With Quote
  #11  
Old July 8th, 2006, 02:31 PM
Mittineague's Avatar
Mittineague Mittineague is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jul 2005
Location: West Springfield, Massachusetts
Posts: 552 Mittineague User rank is Private First Class (20 - 50 Reputation Level)Mittineague User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 9 h 1 m 33 sec
Reputation Power: 10
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.

Reply With Quote
  #12  
Old July 10th, 2006, 09:29 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 14
Quote:
Originally Posted by Mittineague
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.


You've picked a great scenario, however this described behaviour would be better implemented by an internet browser itself, not your web page.

Reply With Quote
  #13  
Old July 18th, 2006, 04:35 AM
Jems Jems is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Location: London, England
Posts: 3 Jems User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 32 m 44 sec
Reputation Power: 0
Question onbeforeunload

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.

Reply With Quote
  #14  
Old July 18th, 2006, 05:15 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,030 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 39 m 41 sec
Reputation Power: 12
Send a message via ICQ to Itsacon
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.

Reply With Quote
  #15  
Old July 18th, 2006, 05:22 AM
Jems Jems is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Location: London, England
Posts: 3 Jems User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 32 m 44 sec
Reputation Power: 0
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.

Reply With Quote
  #16  
Old January 17th, 2007, 10:55 AM
jibouze jibouze is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Posts: 2 jibouze User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 42 sec
Reputation Power: 0
Hi,
you can use the confirm dialogbox.
I do :
Code:
window.onbeforeunload= function (evt) {
if (quit()) {
//do your actions, even launch xmlhttprequest
}
}

function quit() {
if confirm("Quitter ?") {
return true;
}
else {
return false;
}


Hope it will serve.

Reply With Quote
  #17  
Old January 17th, 2007, 10:56 AM
jibouze jibouze is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Posts: 2 jibouze User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 42 sec
Reputation Power: 0
Hi,
you can use the confirm dialog box.
I do :
Code:
window.onbeforeunload= function (evt) {
if (quit()) {
//do your actions, even launch xmlhttprequest
}
}

function quit() {
if confirm("Quitter ?") {
return true;
}
else {
return false;
}


Hope it will serve.

Reply With Quote
  #18  
Old August 10th, 2007, 11:36 AM
AamirRafiq AamirRafiq is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Posts: 1 AamirRafiq User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m
Reputation Power: 0
Check fields value changed in javascript, prevent javascript to close page

Thanks a lot
This really works and i have solved my problem through this code, Thanks once again.
...........................................
Quote:
Originally Posted by KKarthikeyan
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?";
}

Reply With Quote
  #19  
Old July 20th, 2008, 05:04 PM
JOSE JOSE is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: mexico
Posts: 1 JOSE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 59 sec
Reputation Power: 0
Send a message via Yahoo to JOSE
why?

Quote:
Originally Posted by Itsacon
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)


Well Im using it to log the users when they leave a secure site. That way it gets logged at what time they left the site.

Reply With Quote
  #20  
Old September 23rd, 2009, 04:22 AM
MuraliMohanRao MuraliMohanRao is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 1 MuraliMohanRao User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 59 sec
Reputation Power: 0
Hellow Itsacon,

Think in this scenario : Suppose 'X' user is login in to the application and the application wound not allow the same user to login by using another browser. for maintaining this we are updating one flag in the Database while login and logout. once the user close the browser means he can not logout by using the browser. so technically still the user is in logged in state, so application would not allow that user until logout but the user has closed the browser, he can not logged out because browser is not there
So while closing the browser application has to assist to the user by displaying the alert message.

Reply With Quote
  #21  
Old September 23rd, 2009, 07:19 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,030 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2966446 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 1 Week 12 h 39 m 41 sec
Reputation Power: 12
Send a message via ICQ to Itsacon
Nice necro.

Anyway, from your description, you're trying to break proper browser behaviour in order to fix a design flaw in your own system.

Reply With Quote
  #22  
Old October 15th, 2009, 04:35 AM
jerry.daniel123 jerry.daniel123 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 1 jerry.daniel123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 m 58 sec
Reputation Power: 0
Quote:
Originally Posted by MuraliMohanRao
Hellow Itsacon,

Think in this scenario : Suppose 'X' user is login in to the application and the application wound not allow the same user to login by using another browser. for maintaining this we are updating one flag in the Database while login and logout. once the user close the browser means he can not logout by using the browser. so technically still the user is in logged in state, so application would not allow that user until logout but the user has closed the browser, he can not logged out because browser is not there
So while closing the browser application has to assist to the user by displaying the alert message.


hii murali,
did u get the solution to above problem,,,,,do let me know if you get the soln...my gmail id jerry.d23

Reply With Quote
Closed Thread

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > How to stop browser from closing using Javascript?


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap