AllsWeb Blog
No Result
View All Result
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison
Support
Knowledgebase
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison
No Result
View All Result
AllsWeb White Logo
No Result
View All Result
offer web hosting offer web hosting offer web hosting
ADVERTISEMENT
Home JavaScript

Confirmation Before Closing of Tab/Browser using JavaScript

Lakshika Mathur by Lakshika Mathur
January 2, 2020
Reading Time:2min read
0
Confirmation Before Closing of Tab/Browser using JavaScript

In this blog, we’ll explain to you a small JavaScript snippet for showcasing a confirmation dialog box at the time of closing the tab/browser. It is beneficial for both the user and the website. To understand this script correctly, here we take two situations.

RELATED POSTS

Like Dislike Rating System with jQuery, Ajax, and PHP

Star Rating System with jQuery, Ajax, PHP, and MySQL

Create a Digital Clock with Date using JavaScript

Situation 1: In your web project, suppose a user fills up a long-form, but he/she forgot to save the form and then close the browser or tab. In this case, he/she lose all the data, and he/she would not satisfy with your website.

Situation 2: Your website has an extraordinary offer or promotion for the visitors on a page. A visitor visits another page and left from the site without knowing those offers. You lose earning money.

When the user closes the tab/browser, if you implement a confirmation alert, then it will help to solve both the above-described situation. Not only for the above circumstances, but you can also use JavaScript browser close confirmation for many other cases.

With the use of JavaScript onbeforeunload event, you can easily show confirmation on tab close event. JavaScript onbeforeunload event displays a message in a confirmation dialog box to inform the users whether they want to stay or leave the page.

Place the below JavaScript code on the desired webpage. Only change the confMessage variable value as per the suitable message which you want to show to the user.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<script type="text/javascript">
var areYouReallySure = false;
function areYouSure() {
    if(allowPrompt){
        if (!areYouReallySure && true) {
            areYouReallySure = true;
            var confMessage = "***************************************\n\n W A I T !!! \n\nBefore leaving our site, follow CodexWorld for getting regular updates on Programming and Web Development.\n\n\nCLICK THE *CANCEL* BUTTON RIGHT NOW\n\n***************************************";
            return confMessage;
        }
    }else{
        allowPrompt = true;
    }
}
 
var allowPrompt = true;
window.onbeforeunload = areYouSure;
</script>

Also, read our previous blog- Build an HTML5 Video Player with Custom Controls

Tags: JavaScript
ShareTweetSendShareSharePinScan
Lakshika Mathur

Lakshika Mathur

Related Posts

Like Dislike Rating System with jQuery, Ajax, and PHP
jQuery & AJAX

Like Dislike Rating System with jQuery, Ajax, and PHP

January 6, 2020
72
Star Rating System with jQuery, Ajax, PHP, and MySQL
jQuery & AJAX

Star Rating System with jQuery, Ajax, PHP, and MySQL

January 6, 2020
15
Create a Digital Clock with Date using JavaScript
JavaScript

Create a Digital Clock with Date using JavaScript

January 2, 2020
31
Form Validation using jQuery Validation Plugin
jQuery & AJAX

Form Validation using jQuery Validation Plugin

January 2, 2020
5
Display Loading Image While Page Loads using jQuery and CSS
HTML & CSS

Display Loading Image While Page Loads using jQuery and CSS

January 1, 2020
8
File Type (extension) Validation with JavaScript
JavaScript

File Type (extension) Validation with JavaScript

January 1, 2020
3
Next Post
Redirect non-www to www & HTTP to HTTPS using .htaccess file

Redirect non-www to www & HTTP to HTTPS using .htaccess file

Star Rating System with jQuery, Ajax, PHP, and MySQL

Star Rating System with jQuery, Ajax, PHP, and MySQL

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Popular Posts (Last 7 Day)

Like Dislike Rating System with jQuery, Ajax, and PHP

Like Dislike Rating System with jQuery, Ajax, and PHP

January 6, 2020
72
Star Rating System with jQuery, Ajax, PHP, and MySQL

Star Rating System with jQuery, Ajax, PHP, and MySQL

January 6, 2020
15
Redirect non-www to www & HTTP to HTTPS using .htaccess file

Redirect non-www to www & HTTP to HTTPS using .htaccess file

January 2, 2020
4
Confirmation Before Closing of Tab/Browser using JavaScript

Confirmation Before Closing of Tab/Browser using JavaScript

January 2, 2020
19
Build an HTML5 Video Player with Custom Controls

Build an HTML5 Video Player with Custom Controls

January 2, 2020
17
Create a Digital Clock with Date using JavaScript

Create a Digital Clock with Date using JavaScript

January 2, 2020
31

Categories

  • Comparison (2)
  • HTML & CSS (9)
  • JavaScript (27)
    • jQuery & AJAX (18)
  • PHP and MySQL (47)
  • Security (2)
  • SEO (2)
  • Trademark (1)
  • Tutorials (2)
  • Web Hosting (8)
    • VPS Server (2)
  • WordPress (5)

Recommended Stories

Ideas for ten fantastic online business from home

Ideas for ten fantastic online business from home

November 23, 2019
15
Mobile Number Verification via OTP SMS using PHP

Mobile Number Verification via OTP SMS using PHP

December 19, 2019
6
Form Validation using jQuery Validation Plugin

Form Validation using jQuery Validation Plugin

January 2, 2020
5

Popular Stories

  • Ajax Pagination with Search and Filter in PHP

    Ajax Pagination with Search and Filter in PHP

    0 shares
    Share 0 Tweet 0
  • Autocomplete Textbox with Multiple Selection using jQuery in PHP

    0 shares
    Share 0 Tweet 0
  • Image Gallery CRUD with PHP and MySQL

    0 shares
    Share 0 Tweet 0
  • One Time Temporary Download Link with Expiration in PHP

    0 shares
    Share 0 Tweet 0
  • Create PDF with Watermark in PHP using Dompdf

    0 shares
    Share 0 Tweet 0
  • Home
  • Posts
  • Privacy Policy
  • Terms and Conditions

Built and Maintained With ♥ by AllsWeb Team

No Result
View All Result
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison

Built and Maintained With ♥ by AllsWeb Team