Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix(hint): properly clean up when location is changed before DOM cont…
Browse files Browse the repository at this point in the history
…ent 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
  • Loading branch information
gkalpak committed May 2, 2017
1 parent aab2964 commit 97c16a2
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions hint.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,26 @@ if (deferRegex.test(window.name)) {
}
//If this is not a test, defer bootstrapping
else {
var isDeferLabelRemoved = false;
window.name = DEFER_LABEL + window.name;

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

/* angular should remove DEFER_LABEL from window.name, but if angular is never loaded, we want
to remove it ourselves, otherwise hint will incorrectly detect protractor as being present on
the next page load */
window.addEventListener('beforeunload', function() {
if (deferRegex.test(window.name)) {
// AngularJS should remove `DEFER_LABEL` from `window.name`, but if AngularJS is never loaded,
// we want to remove it ourselves. Otherwise `hint` will incorrectly detect protractor as being
// present on the next page load.
// Generally, the `unload` event is fired more consistently, but we also use `beforeunload` to
// cover potential browser bugs/edge-cases.
window.addEventListener('unload', removeDeferLabelOnce);
window.addEventListener('beforeunload', removeDeferLabelOnce);

function removeDeferLabelOnce() {
if (!isDeferLabelRemoved && deferRegex.test(window.name)) {
window.name = window.name.substring(DEFER_LABEL.length);
isDeferLabelRemoved = true;
}
});
}
}

function maybeBootstrap() {
Expand Down

0 comments on commit 97c16a2

Please sign in to comment.