Skip to content

Commit 97c16a2

Browse files
committedMay 2, 2017
fix(hint): properly clean up when location is changed before DOM content has been loaded
The `beforeunload` event was never fired if the location was changed before the DOM content had been loaded (e.g. directly inside a `<script>` tag). The `unload` event is fired more consistently. This commit listens for both events to cover more potential browser bugs/edge-cases. Fixes angular/batarang#290
1 parent aab2964 commit 97c16a2

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed
 

‎hint.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,26 @@ if (deferRegex.test(window.name)) {
4646
}
4747
//If this is not a test, defer bootstrapping
4848
else {
49+
var isDeferLabelRemoved = false;
4950
window.name = DEFER_LABEL + window.name;
5051

5152
// determine which modules to load and resume bootstrap
5253
document.addEventListener('DOMContentLoaded', maybeBootstrap);
5354

54-
/* angular should remove DEFER_LABEL from window.name, but if angular is never loaded, we want
55-
to remove it ourselves, otherwise hint will incorrectly detect protractor as being present on
56-
the next page load */
57-
window.addEventListener('beforeunload', function() {
58-
if (deferRegex.test(window.name)) {
55+
// AngularJS should remove `DEFER_LABEL` from `window.name`, but if AngularJS is never loaded,
56+
// we want to remove it ourselves. Otherwise `hint` will incorrectly detect protractor as being
57+
// present on the next page load.
58+
// Generally, the `unload` event is fired more consistently, but we also use `beforeunload` to
59+
// cover potential browser bugs/edge-cases.
60+
window.addEventListener('unload', removeDeferLabelOnce);
61+
window.addEventListener('beforeunload', removeDeferLabelOnce);
62+
63+
function removeDeferLabelOnce() {
64+
if (!isDeferLabelRemoved && deferRegex.test(window.name)) {
5965
window.name = window.name.substring(DEFER_LABEL.length);
66+
isDeferLabelRemoved = true;
6067
}
61-
});
68+
}
6269
}
6370

6471
function maybeBootstrap() {

0 commit comments

Comments
 (0)
Please sign in to comment.