prevent-unload
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -14,2 +14,3 @@ 'use strict'; | ||
listener = function listener(event) { | ||
// for cross-browser solution, see: https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload | ||
event.returnValue = message; | ||
@@ -16,0 +17,0 @@ return message; |
{ | ||
"name": "prevent-unload", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Prevent page unloading in simple way", | ||
"main": "./lib/index.js", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
@@ -30,8 +30,8 @@ "test": "echo \"Error: no test specified\" && exit 1", | ||
"devDependencies": { | ||
"babel-eslint": "^5.0.0-beta6", | ||
"babel-eslint": "^6.0.0-beta.6", | ||
"babel-plugin-add-module-exports": "^0.1.2", | ||
"babel-preset-es2015": "^6.3.13", | ||
"eslint": "^2.0.0-alpha-2", | ||
"eslint-plugin-babel": "^3.0.0" | ||
"babel-preset-es2015": "^6.6.0", | ||
"eslint": "^2.4.0", | ||
"eslint-plugin-babel": "^3.1.0" | ||
} | ||
} |
const defaultMessage = 'Are you sure?'; | ||
let listener = null; | ||
let listener; | ||
function preventUnload(message = defaultMessage) { | ||
listener = (event) => { | ||
// for cross-browser solution, see: https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload | ||
event.returnValue = message; | ||
return message; | ||
}; | ||
if (listener) { | ||
return; | ||
} | ||
listener = makeHandler(message); | ||
window.addEventListener('beforeunload', listener); | ||
@@ -16,8 +15,18 @@ } | ||
function revoke() { | ||
if (listener) { | ||
window.removeEventListener('beforeunload', listener); | ||
listener = null; | ||
if (!listener) { | ||
return; | ||
} | ||
window.removeEventListener('beforeunload', listener); | ||
listener = null; | ||
} | ||
function makeHandler(message) { | ||
return () => { | ||
// for cross-browser solution, see: https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload | ||
event.returnValue = message; | ||
return message; | ||
}; | ||
} | ||
export { | ||
@@ -24,0 +33,0 @@ preventUnload as default, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9481
50