Skip to content

Instantly share code, notes, and snippets.

@romainbessugesmeusy
Last active July 30, 2019 12:20
Show Gist options
  • Save romainbessugesmeusy/3c30288149ae84d95c05591f8c817a33 to your computer and use it in GitHub Desktop.
Save romainbessugesmeusy/3c30288149ae84d95c05591f8c817a33 to your computer and use it in GitHub Desktop.
iFrame event propagation
<html>
<head>
<title>Host page</title>
</head>
<body>
<h1>My home page</h1>
<iframe src="partner_iframe.html"></iframe>
<script type="text/javascript">
// by declaring an "axeptio" callback function in the DOM,
// you receive a reference to the loaded SDK once its ready
// *warning* do not declare several axeptio function because only
// the last one will get called.
window.axeptio = function(ax) {
ax.on('cookies:complete', function(choices) {
// we iterate through every frame present in the document
// and call the postMessage method to send choices event
let i = 0;
for(i; i < frames.length; i++){
frames[i].postMessage(Object.assign({
isAxeptioEvent: true,
event: 'cookies:complete',
choices: choices
}))
}
})
};
</script>
<!-- Axeptio SDK -->
<script type="text/javascript">
var el = document.createElement('script');
el.setAttribute('src', 'https://static.axept.io/sdk.js');
el.setAttribute('type', 'text/javascript');
el.setAttribute('async', true);
el.setAttribute('data-id', '5a6b407f9cffce2ad8a02684');
if (document.body !== null) {
document.body.appendChild(el);
}
</script
</body>
</html>
<html>
<head>
<title>Host Iframe</title>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-5R7JNFX');</script>
<!-- End Google Tag Manager -->
<!-- Axeptio remot GTM trigger -->
<script>
(function(w) {
function gtm(choices) {
if (!choices) return;
w.dataLayer = w.dataLayer || [];
Object.keys(choices).forEach(function(vendor) {
if (choices[vendor]) {
w.dataLayer.push({ event: 'axeptio_activate_' + vendor });
w.dataLayer.push({ event: 'axeptio_enable_' + vendor });
} else {
w.dataLayer.push({ event: 'axeptio_disable_' + vendor });
}
});
}
function onEvent(event) {
if (!event.data.isAxeptioEvent) return;
if (event.data.event === 'cookies:complete') {
gtm(event.data.choices);
}
}
w.addEventListener('message', onEvent ,false);
})(window);
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment