I have the following code, my question is that, is there a way I can cancel the onbeforeunload custom pop up from inside the method call? In case of firefox I want to show my own custom message and if the user pressed cancel I dont want to show the default message.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var clicked = false;
function copyText()
{
var span = document.getElementById('sp');
if(clicked == false)
{
clicked = true;
span.appendChild( document.createTextNode("clicked") );
}
else
{
clicked = false;
span.removeChild( span.firstChild );
}
}
window.onbeforeunload = function () {
if(clicked == true)
return ;
else
{
if(navigator.userAgent.indexOf("Firefox")!=-1)
{
if(confirm("Are you sure you want to close the window without submitting the documents?")){
self.close();
return; // I want to not call the custom pop up here
}
}
return "Are you sure you want to close the window without submitting the documents?";
}
};
//}
</script>
</head>
<!--<body onunload="confirmMe()>-->
<body>
<button onclick="copyText()">Click!</button>
<br>
<span style="color:lightblue" id="sp"></span>
</body>
</html>
There’s no way you can cancel the onbeforunload event from inside the function.
But the following will show the default popUp if the user doesn’t want to close the page, it only happens in Firefox if user wants to stay on page.