jquery.confirmExit.js 837 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*!
  2. * jQuery confirmExit plugin
  3. * https://github.com/dunglas/jquery.confirmExit
  4. *
  5. * Copyright 2012 Kévin Dunglas <dunglas@gmail.com>
  6. * Released under the MIT license
  7. * http://www.opensource.org/licenses/mit-license.php
  8. */
  9. (function ($) {
  10. $.fn.confirmExit = function(message) {
  11. var confirmExit = false;
  12. $('input, textarea, select', this).on('change keyup', function() {
  13. // Do not set the event handler if not needed
  14. if (!confirmExit) {
  15. confirmExit = true;
  16. window.onbeforeunload = function(event) {
  17. var e = event || window.event;
  18. // For old IE and Firefox
  19. if (e) {
  20. e.returnValue = message;
  21. }
  22. return message;
  23. }
  24. }
  25. });
  26. this.submit(function() {
  27. window.onbeforeunload = null;
  28. confirmExit = false;
  29. });
  30. return this;
  31. }
  32. })(jQuery);