Skip to main content

Modal window with browser close event


Prompt a window at the time of logout


You can download the complete Project from here

We all come across scenarios where a modal window needs to be popped up at the event of logout, you can do a simple alert box, confirm box or stylish jquery plugins available on the internet.

For this tutorial I am referring to a superb simple light weight jquery modal window by Eric Martin, I do not own this library, I have just implemented it and other add on utilities.

Lets get started, so requirements:
1) User should see a light box modal window at the time of logout.
2) The pop up window should have a confirmation box stating yes or no.
3) Click No and window should remain intact.
4) Click yes, background operations, server calls, document write etc.

If log out operation is not performed, user while navigating away from the current window should be prompted, like "do you want to navigate away from this window or stay on this window"
.
Catch: pressing f5 does not trigger this navigation away(handled in the javascript shown below), once the user clicked on yes logout, again the navigation event is unbinded.

To install the Jquery modal window plugin you can refer to the plugin and license below.

/*
 * SimpleModal 1.4.1 - jQuery Plugin
 * http://www.ericmmartin.com/projects/simplemodal/
 * Copyright (c) 2010 Eric Martin (http://twitter.com/ericmmartin)
 * Dual licensed under the MIT and GPL licenses
 * Revision: $Id: jquery.simplemodal.js 261 2010-11-05 21:16:20Z emartin24 $
 */


Javascript for the browser close, preventing browser to get closed, triggering the modal window is shown below or alternatively you can download the complete package from Google Project Hosting




 function setConfirmUnload(on) {
    
    window.onbeforeunload = (on) ? unloadMessage : null;
    }


function unloadMessage() {
    return 'This page is very critical to track the usage, navigating away may incur charges...'; 
      }




function closeWindow() {
    //Mozilla Fix
    if (window.netscape)
      netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite");
    window.open('','_self');
    window.close();
}


jQuery(function ($) {


$('#basic-modal .basic').click(function (e) {
  
$('#basic-modal-content').modal({
    containerCss:
    { maxHeight: 250
    } ,
    
    onOpen: function (dialog) 
    {
         dialog.overlay.fadeIn('slow', function () {
        dialog.container.slideDown('slow', function () {
        dialog.data.fadeIn('slow')
      ;});});
    },
    
    onClose: function (dialog) 
    {
  dialog.data.fadeOut('slow', function () {
 dialog.container.hide('slow', function () {
dialog.overlay.slideUp('slow', function () {
$.modal.close();
});
;});});
    }
    
    });
return false;
});
  
  
  
  $('#logouttrue').click(function (e) {
     setConfirmUnload(false);
$('#content').html("You are done now, you can <a href='#' id='noclose' onclick='javascript:closeWindow();'>Close it..</a>");
    
});
  
  setConfirmUnload(true);
              
});
//keydown works with Chrome and IE
$(document).keydown(function(event){
      //alert(event.keyCode);
     if(event.keyCode == 116)
     setConfirmUnload(false);
      });



Again I would like to thank all the guys who post such great plugins and codes.

Happy Coding!!

Comments

Popular posts from this blog

SugarCRM Footer Logo Remove

Hi Googler, Let us discuss about how to remove the SugarCRM Footer logo.. You all are aware of the power of the SugarCRM Tool and also must be very eager to remove the footer in order to make it look more professional. So below mentioned are some tricks for removing the footer from SugarCRM Community edition. 1) O pen-modules/Administration/ updater_utils.php Add-exit() ; in between function check_now() and return . By doing this u can remove 'powered by sugar crm' footer logo. 2) Go to modules/Trackers/Tracker.php, line 128, in the 'logPage' function. Drop the 'echo' statement. 3) Now to root(Sugarfolder)\include\mvc\view\sugarview.php and modify the line array(show header => true, show subpanel => true...........and so on) and make the changes to showfooter=>true to showfooter=>false And then enjoy by making it as your own built tool. As per the requests, make a reply and then I'll post the code and path to let you know how to write a cust...
Merry Christmas

Custom Create React App | Best Practices in Typescript | React >16

Custom Create React App | Best Practices in Typescript | React >16 React app has multiple versions now a days, based on the latest typescript implementation using React 16. Here is a code sample if it helps anyone to Kickstart their project or take "inspiration"  GitHub Project This is a simple app to get gists of user. React app, Single Page, Gist User Data download code in local via -  https://github.com/jatinmarwah/React_Sample Master - latest Stable Develop - ongoing check in (might be unstable) execute command on root folder yarn run build:dev OR npm run build: dev Start app with yarn start OR npm start; GO TO: "localhost:8080" Test with any valid gist username App Tested with user "paulirish" Data validation No extensive data validation is done, pagination is not supported(YET), max result received is 30 Basic validations and error handling is done, app level handling and boundaries can be extended Caution As un-authenticated api is being used, GI...