Now here in this tutorial, I’ll explain how to prompt alert before page leave ( user leaves the page ) or close the webpage tan in javascript as well as jquery with example code. If you liked this tutorial after going through it, please feel free to check out our other tutorials as well because they are highly valuable in helping you become a better coder.
In my previous tutorials, I’d explained how to show alert message from code-behind, how to call javascript function from code-behind, confirm message box example using javascript and other more cracking tutorials on Asp.net, JavaScript, jQuery here.
Alert Before Page Leave In JavaScript
Following is the JavaScript Code to show alert before page leave or closing current window:
//By using JavaScript
window.onbeforeunload = function () {
return ‘please save your setting before leaving the page’;
}
</script>
Alert Before Page Leave In jQuery
Following is the jQuery Code to show alert before page leave or closing current window:
</script>
<script type=”text/javascript”>
//By using jQuery
$(window).bind(‘beforeunload’, function () {
return ‘please save your setting before leaving the page.’;
});
</script>
Show Alert Before Current Window Close – [.aspx]
Following is the complete HTML Markup code for .aspx page:
<head id=”Head1″ runat=”server”>
<title>Prompt alert before leaving webpage using JavaScript
in Asp.net </title>
<script type=”text/javascript”>
//By using JavaScript
window.onbeforeunload = function () {
return ‘please save your setting before leaving the page’;
}
</script>
</head>
<body>
<form id=”form1″ runat=”server”>
<h4>
Show alert message box before leaving webpage using JavaScript
in Asp.net</h4>
<div>
close this tab to test live demo
</div>
</form>
</body>
</html>