Skip to content

Commit 07026c5

Browse files
committed
Fixed initialization in iframes
- The widget is now not displayed inside iframes if the iframe is displayed on the same origin as the main window. This prevents the widget from being displayed multiple times. - The widget will wait for the `DOMContentLoaded` event to occur if `document.body` does not yet exist.
1 parent 3f5957d commit 07026c5

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
### Fixed
10+
- The widget is now not displayed inside iframes if the iframe is displayed on the same origin as the main window. This prevents the widget from being displayed multiple times.
11+
- The widget will wait for the `DOMContentLoaded` event to occur if `document.body` does not yet exist.
912

1013
## [1.4.0] - 2025-06-17
1114
### Added

src/CookieConsentWrapper.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ export class CookieConsentWrapper {
379379

380380
let initPromise = null;
381381

382-
if (!this._config.pluginOptions.init_after_dom_content_loaded) {
382+
if (!this._config.pluginOptions.init_after_dom_content_loaded && document.body) {
383383
initPromise = doInitCookieConsent();
384384
}
385385

src/CookieConsentWrapperFactory.mjs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,26 @@ export class CookieConsentWrapperFactory {
3939
},
4040
}
4141

42-
cookieConsentWrapper.init(window, document);
42+
if (this.#shouldInit()) {
43+
cookieConsentWrapper.init(window, document);
44+
}
45+
4346

4447
return cookieConsentWrapper;
4548
}
4649

50+
#shouldInit() {
51+
try {
52+
if (window.self === window.top) {
53+
return true;
54+
}
55+
56+
return !window.top.location.hostname;
57+
} catch (e) { // eslint-disable-line no-unused-vars
58+
return true;
59+
}
60+
}
61+
4762
#createGtagFunction() {
4863
let gtag = window.gtag;
4964

0 commit comments

Comments
 (0)