I'm trying to set up a referral program for users of a site. Each one currently gets a referral link like this: mysite.app/user/signup/?ref=123456789
On each signup, I need to slice that URL ?ref parameter 123456789
, assign it to the variable ref
and push it to a form input field with the id=hiddenref
.
I have the script below from a tutorial but I can't get it to work and I suspect it might be because it was meant for a URL with a folder structure like this: mysite.app/?ref=123456789
.
Any help would be appreciated!
<script>
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = window.location.search.substring(1),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
}
else return "none";
}
};
ref = getUrlParameter('ref');
document.getElementById('hiddenref').value = ref;
</script>