Home Asp.net Prompt Alert Before Page Leave in JavaScript or jQuery

Prompt Alert Before Page Leave in JavaScript or jQuery

0
0

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:

<script type=”text/javascript”>
//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 type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js”>
</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:

<html xmlns=”http://www.w3.org/1999/xhtml”>
<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>
Note: Here i’d used JavaScript method to prompt alert message but you can also use jQuery mehod if you like.

Sample Live Demo

Example Result

Prompt alert box before leaving webpage using javascript in asp.net?

LEAVE A REPLY

Please enter your comment!
Please enter your name here
Captcha verification failed!
CAPTCHA user score failed. Please contact us!