Monday, 28 April 2014

jQuery Show Alert Message before Leaving (Closing) from Web Page


Introduction

Here I will explain how to use 
jQuery to show alert message before leaving from web page or jQueryshow alert message while leaving from web page or closing web page using beforeunload method.

Description
  
In previous articles I explained jQuery open all external links in new window
jQuery enable / disable controls on pagejQuery enable or disable hyperlinksjQuery Countdown timer script examplejQuery Increase or Decrease font size of website and many articles relating to JQuery and asp.net. Now I will explain how to use jQuery to show alert message before leaving from web page.

To show alert message while leaving from webpage we need to write the code like as shown below


<script type="text/javascript">
$(function() {
$(window).on('beforeunload',function() {
return '';
});
});
</script>
If you want to see this with complete example check below code

Example


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Show Alert Message while leaving from webpage</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function() {
$(window).on('beforeunload'function() {
return '';
});
});
</script>
</head>
<body>
<form id="form1">
<div>
<b>Welcome to Aspdotnet-suresh.com</b>
</div>
</form>
</body>
</html>

No comments:

Post a Comment