Last active
August 25, 2017 02:04
-
-
Save JiaLiPassion/7dcc02eda8d1fa79588f6dcf6f7b06a9 to your computer and use it in GitHub Desktop.
Sample to describe how to use `onbeforeunload` without `event` parameter in `zone.js`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="./zone.js"></script> | |
<script> | |
const desc = Object.getOwnPropertyDescriptor(window, 'onbeforeunload'); | |
if (desc) { | |
const oriGet = desc.get; | |
const oriSet = desc.set; | |
const preDefinedEvent = document.createEvent('Event'); | |
preDefinedEvent.initEvent('beforeunload', false, true); | |
Object.defineProperty(window, 'onbeforeunload', { | |
configurable: true, | |
get: function () { | |
const wrapFn = oriGet.apply(this, arguments); | |
if (wrapFn) { | |
return function() { | |
const args = Array.prototype.slice.call(arguments); | |
let evt = args.length === 0 ? preDefinedEvent : args[0]; | |
return wrapFn.apply(this, [evt]); | |
} | |
} | |
return wrapFn; | |
} | |
}); | |
} | |
} | |
window.onbeforeunload = function() { | |
console.log('test', arguments[0]); | |
} | |
window.onbeforeunload(); | |
</script> | |
</head> | |
<body> | |
<button type="button" id="btn">btn</button> | |
</body> | |
</html>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment