Skip to content

Instantly share code, notes, and snippets.

@hristiank
Created October 21, 2019 13:15
Show Gist options
  • Save hristiank/5feb3bde8bcee4dddfd20b4fa1fd126d to your computer and use it in GitHub Desktop.
Save hristiank/5feb3bde8bcee4dddfd20b4fa1fd126d to your computer and use it in GitHub Desktop.
<!-- IFRAME IMPLEMENTATION -->
<script>
function ready (fn) {
if (document.readyState !== 'loading') {
fn()
} else {
document.addEventListener('DOMContentLoaded', fn)
}
}
ready(function () {
parent.postMessage('{ "status": "OK" }', '*')
})
function propertyToField (property) {
var config = {
variantId: 'variant_letter',
utm_source: 'utm_source',
utm_medium: 'utm_medium',
utm_campaign: 'utm_campaign'
}
return config[property]
}
function fillData (data) {
for (var i in data) {
var field = propertyToField(i)
var element = document.getElementById(field)
if (element) {
element.value = data[i]
}
}
}
window.addEventListener('message', function (event) {
fillData(event.data)
}, false)
</script>
<!-- PARENT IMPLEMENTATION -->
<script>
function queryStringToJSON () {
var pairs = window.location.search.slice(1).split('&')
var result = {}
pairs.forEach(function (pair) {
pair = pair.split('=')
if (pair.length === 2)
result[pair[0]] = decodeURIComponent(pair[1] || '')
})
return result
}
var query = queryStringToJSON()
var preparedData = Object.assign({}, query, window.ub && window.ub.page)
window.addEventListener('message', function (event) {
var data = JSON.parse(event.data)
if (data && data.status === 'OK') {
var iframe = document.querySelector('iframe').contentWindow
iframe.postMessage(preparedData, '*')
}
}, false)
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment