Skip to content

Instantly share code, notes, and snippets.

@KustomDeveloper
Last active January 25, 2018 19:08
Show Gist options
  • Select an option

  • Save KustomDeveloper/5cf495648154760a5740f735eb49ae41 to your computer and use it in GitHub Desktop.

Select an option

Save KustomDeveloper/5cf495648154760a5740f735eb49ae41 to your computer and use it in GitHub Desktop.
Allows me to set any query value in the pages query string then rebuild the iframe and set value for hidden fields
<noscript>
<iframe src="YOUR_IFRAME_URL" scrolling="no" type="text/html" frameborder="0" allowTransparency="true" style="width:300px;height:100%;overflow:hidden;border: 0"></iframe>
</noscript>
<script type="text/javascript">
// Add / Update a key-value pair in the URL query parameters
function updateUrlParameter(uri, key, value) {
// remove the hash part before operating on the uri
var i = uri.indexOf('#');
var hash = i === -1 ? '' : uri.substr(i);
uri = i === -1 ? uri : uri.substr(0, i);
var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
var separator = uri.indexOf('?') !== -1 ? "&" : "?";
if (uri.match(re)) {
uri = uri.replace(re, '$1' + key + "=" + value + '$2');
} else {
uri = uri + separator + key + "=" + value;
}
return uri + hash; // finally append the hash as well
}
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var queryString = window.location.search;
var fullUrl = window.location.href;
var lpVariant = 'Variant A';
var basicUrl = [location.protocol, '//', location.host, location.pathname].join('');
var importantInfo = basicUrl + ' ' + lpVariant;
var updatedUrl = updateUrlParameter(fullUrl, 'source', importantInfo);
window.history.replaceState( {} , 'source', updatedUrl);
var utm_source = getParameterByName('utm_source');
var utm_medium = getParameterByName('utm_medium');
var utm_campaign = getParameterByName('utm_campaign');
var utm_term = getParameterByName('utm_term');
var source = getParameterByName('source');
var utmFields = 'utm_source:' + utm_source + ', utm_medium:' + utm_medium + ', utm_campaign:' + utm_campaign + ', utm_term:' + utm_term;
var finalUrl = updateUrlParameter(updatedUrl, 'query', utmFields);
console.log(utmFields);
window.history.replaceState( {} , 'query', finalUrl);
//Begin rebuilding iframe
var form = 'YOUR_IFRAME_URL';
var params = window.location.search;
var thisScript = document.scripts[document.scripts.length - 1];
var iframe = document.createElement('iframe');
iframe.setAttribute('src', form + params);
iframe.setAttribute('width', '270px');
iframe.setAttribute('height', '100%');
iframe.setAttribute('type', 'text/html');
iframe.setAttribute('frameborder', 0);
iframe.setAttribute('allowTransparency', 'true');
iframe.setAttribute('scrolling', 'no');
iframe.style.border = '0';
thisScript.parentElement.replaceChild(iframe, thisScript);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment