Skip to content

Instantly share code, notes, and snippets.

@acoyfellow
Last active July 14, 2018 11:21
Show Gist options
  • Save acoyfellow/36e0f2cdfe51f5a01c922dd164eaefcf to your computer and use it in GitHub Desktop.
Save acoyfellow/36e0f2cdfe51f5a01c922dd164eaefcf to your computer and use it in GitHub Desktop.
For trial sales page. Will inject + lock down any form field that is sent in through the URL (by the input ID)
<script>
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, " "));
};
function createCookie(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}else{
expires = "";
};
document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/";
};
jQuery(document).ready(function () {
document.querySelectorAll('input').forEach(function(input){
if(input.id && getParameterByName(input.id)){
jQuery('#'+input.id).val(getParameterByName(input.id));
jQuery('#'+input.id).prop('disabled', true);
};
});
//if affiliate in url, set to cookies
var affiliate = getParameterByName('affiliate');
if(affiliate){
createCookie('affiliate', affiliate, 30);
};
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment