Search This Blog

Monday 30 April 2012

How to confirm before close browser using javscript?


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>

No comments:

Post a Comment