I am using following code so that if user closes the browser button it shows a confirmation box, whether to stay on page or not.
What I want to do is if someone closes the browser it should show an image, with “yes” or “no” options. When “yes” is clicked it should close the browser and if “no” is clicked it shouldn’t close the browser. The Image has to be shown in the same window not in any popup. Is it possible to do it or am I expecting too much from JavaScript?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
var exit=true;
function confirmExit()
{
if(exit)
{
window.location.href = "?p=exit";
}
if(exit)
return "Wait! Don't Leave Empty Handed!\n\nThank you for taking the time to check out our offer! Before you go we have a complimentary crash to help you succeed. Click the 'Cancel' or 'Stay On This Page' button if you're interested!";
}
</script>
</head>
<body onbeforeunload="return confirmExit()">
</body>
</html>
You cannot change the
onbeforeunload()default behaviour as it is native to each browser. You can only return a custom string.