Home > Javascript > How to confirm before close browser using javascript?

How to confirm before close browser using javascript?


Can we stop browser close using Javascript? 

Some time it’s better to give a warning to user when he trying to close the browser. Some browsers has the facility by default but they can be disabling from the browser itself. So it’s better to give a warning message to user from the application itself. In this case we can use JavaScript for given the confirm message to user before close the browser window.

 How to stop browser closing using javscript ?

We have to call a javascript funtion to give a confirmation message to user on browser closing event. We can achieve it by call a event ‘onbeforeunload’ from the body tag. This event will fired when a user close the browser. On this event we are calling a function quit(), On this function we are given a confirm message to user and return the result. If user clicks OK browser will close, if user press cancel it will return to the browser

 Simple Javascript codes to prevent browser close

 
<html>
<head>
<script type="text/javascript">
function quit(){
event.returnValue="Are you sure you have finished?"
}
</script>
</head>
<body onbeforeunload="quit()">
<div id="timeDiv"></div>
</body>
</html>
Categories: Javascript Tags:
  1. Johnc327
    May 19, 2014 at 8:09 am

    I’m hunting for web sites that contain fantastic recommendations on what’s in fashion and just what top rated makeup products is.. eceagkgkdgdg

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: