Skip to content

Commit

Permalink
The original plugin is initialized directly if the DOM is loaded or i…
Browse files Browse the repository at this point in the history
…nside a callback attached to an event `document.DOMContentLoaded` instead of `window.load`
  • Loading branch information
tg666 committed Jan 8, 2022
1 parent e92422d commit 19a2388
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added the section `How the GTM integration works` in README
- Added the section `How to update already published container` in README

### Changed

- The original plugin is initialized directly if the DOM is loaded or inside a callback attached to an event `document.DOMContentLoaded` instead of `window.load`.

## [0.3.4] - 2021-01-05

### Removed
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "68publishers-cookie-consent",
"version": "0.3.4",
"version": "0.3.5-beta.1",
"description": "Cookie consent wrapper based on orestbida/cookieconsent with GTM integration.",
"homepage": "http://www.68publishers.io/",
"main": "index.js",
Expand Down
24 changes: 9 additions & 15 deletions src/CookieConsentWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,14 @@ class CookieConsentWrapper {
const self = this;
window.CookieConsentWrapper = self;

if (document) {
// load stylesheets
const documentLoadedCallback = function () {
StylesheetLoader.loadFromConfig(document, self._config.uiOptions);
};

if ('loading' !== document.readyState) {
documentLoadedCallback();
} else {
document.addEventListener('DOMContentLoaded', documentLoadedCallback);
}
if (!document) {
return;
}

const windowLoadedCallback = function () {
const documentLoadedCallback = function () {
// load stylesheets
StylesheetLoader.loadFromConfig(document, self._config.uiOptions);

// init cookie consent
self._cookieConsent = initCookieConsent();
const consentManager = new ConsentManager(self._cookieConsent, self._config, self._storagePool, Object.values(self._eventTriggers), self._gtag);
Expand Down Expand Up @@ -146,10 +140,10 @@ class CookieConsentWrapper {
self._eventBus.dispatch(Events.ON_INIT);
};

if ('complete' === document.readyState) {
windowLoadedCallback();
if ('loading' !== document.readyState) {
documentLoadedCallback();
} else {
window.addEventListener('load', windowLoadedCallback);
document.addEventListener('DOMContentLoaded', documentLoadedCallback);
}

this._initialized = true;
Expand Down

0 comments on commit 19a2388

Please sign in to comment.