1

I am working on a Javascript that is suppose to do a click feature on an element as well as showing a pop-up asking if you want to really leave the site (close the tab). Now The code works fine on IE and Firefox. But Chrome while it does do the important thing in terms of doing the click(); It will not show a pop-up asking if I want to leave or not. I Don't know if its a feature I need to enable in the Chrome browser or something else. Here is the code I am using. Any help would be much appreciated.

var validNavigation = false;

function wireUpEvents() {

  var dont_confirm_leave = 0; 
var leave_message = document.getElementById("kioskform:broswerCloseSubmit");
      function goodbye(e) {
        if (!validNavigation) {
          if (dont_confirm_leave!==1) {
            if(!e) e = window.event;
            //for IE
            e.cancelBubble = true;
            e.returnValue = leave_message.click();
            //e.stopPropagation works in Firefox.
            if (e.stopPropagation) {
              e.stopPropagation();
              e.preventDefault();
            }
            //return works for Chrome and Safari
            return leave_message.click();
            alert("Removing information.");
            //add the code to delete the kiosk information here.
            // this is what is to be done.
          }
        }
      }
      window.onbeforeunload=goodbye;

      // Attach the event keypress to exclude the F5 refresh
      jQuery('document').bind('keypress', function(e) {
        if (e.keyCode == 116){
          validNavigation = true;
        }
      });

      // Attach the event click for all links in the page
      jQuery("a").bind("click", function() {
        validNavigation = true;
      });

      // Attach the event submit for all forms in the page
     jQuery("form").bind("submit", function() {
        validNavigation = true;
      });

      // Attach the event click for all inputs in the page
     jQuery("input[type=submit]").bind("click", function() {
        validNavigation = true;
      });

    }

    // Wire up the events as soon as the DOM tree is ready
    jQuery(document).ready(function() {
      wireUpEvents();
    });
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Gilbert V
  • 1,050
  • 5
  • 16
  • 43

1 Answers1

2

You have to return a string in the onbeforeunload function to show the message to the user, see also Setting onbeforeunload on body element in Chrome and IE using jQuery

Community
  • 1
  • 1
daolaf
  • 167
  • 4
  • So you mean instead of using return leave_message.click(); I use return "are you sure"; and that will do it? Will the click feature still work on Chrome and Safari if I do it that way? – Gilbert V Sep 21 '12 at 18:48
  • 1
    The click will still work if you call it before the return. So first call the click, then return "you sure?". – daolaf Sep 21 '12 at 18:54
  • Sweet thank you that worked out fine. Say is there a reason why the click isn't working in safari? – Gilbert V Sep 21 '12 at 19:18