How to stop browser from closing using Javascript?
Discuss How to stop browser from closing using Javascript? in the JavaScript Development forum on Dev Articles. How to stop browser from closing using Javascript? JavaScript Development forum discussing the use of JavaScript and its features as a powerful DOM manipulator. JavaScript is used in most websites to enrich the interface and enhance the user experience.
Posts: 1
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.
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
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");
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>
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
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.";
}
Posts: 1
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");
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>
Posts: 1
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 10:25 AM.
Reason: need a change in subject
Posts: 553
Time spent in forums: 1 Week 1 Day 9 h 1 m 33 sec
Reputation Power: 8
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.
Posts: 1,030
Time spent in forums: 1 Week 12 h 34 m 39 sec
Reputation Power: 10
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
Posts: 469
Time spent in forums: 2 Days 1 h 36 m 37 sec
Reputation Power: 7
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
Posts: 553
Time spent in forums: 1 Week 1 Day 9 h 1 m 33 sec
Reputation Power: 8
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.
Posts: 2,886
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 13
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.
Posts: 3
Time spent in forums: 1 h 32 m 44 sec
Reputation Power: 0
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.
Posts: 2
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;
}
Posts: 2
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;
}
Posts: 1
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?";
}
Posts: 1
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.
Posts: 1
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