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
Home JavaScript

Confirmation Before Closing of Tab/Browser using JavaScript

Lakshika Mathur by Lakshika Mathur
January 2, 2020
Reading Time: 2 mins 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.

<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
785
Star Rating System with jQuery, Ajax, PHP, and MySQL
jQuery & AJAX

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

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

Create a Digital Clock with Date using JavaScript

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

Form Validation using jQuery Validation Plugin

January 2, 2020
35
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
46
File Type (extension) Validation with JavaScript
JavaScript

File Type (extension) Validation with JavaScript

January 1, 2020
44
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 *

Categories

  • Comparison (3)
  • HTML & CSS (9)
  • Interesting Facts (1)
  • JavaScript (27)
    • jQuery & AJAX (18)
  • PHP and MySQL (48)
  • Security (10)
  • SEO (2)
  • Trademark (2)
  • Tutorials (5)
  • Uncategorized (1)
  • Web Hosting (19)
    • VPS Server (5)
  • WordPress (8)

Recent Posts

  • Is the Trademark valuable to your Brand or domain?
  • Ideas For Ten Fantastic Online Business From Home
  • Some best free WordPress Themes for Affiliate Marketing Websites
  • 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

Go to mobile version