Sign up for APLF's E-newsletter
// We are waiting for the full page to load before loading this feature. This prevents a few bugs.
window.addEventListener('load', function () {
var queryForm = function(settings) {
// Parsing URL
var reset = settings && settings.reset ? settings.reset : false;
var self = window.location.toString();
var querystring = self.split("?");
// Checking for parameters in the URL
if (querystring.length > 1) {
var pairs = querystring[1].split("&");
// Iterating through all key-value pairs
for (i in pairs) {
var keyval = pairs[i].split("=");
// Condition to only save "utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content" in cookies
if (reset || sessionStorage.getItem(keyval[0]) === null) {
if (["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"].includes(keyval[0])) {
const d = new Date();
// Setting cookie expiration time to 14 days
d.setTime(d.getTime() + (14 * 24 * 60 * 60 * 1000));
let expires = "expires=" + d.toUTCString();
// Setting the cookie
document.cookie = keyval[0] + "=" + keyval[1] + ";" + expires + ";path=/";
}
}
}
}
// Function to get the value of a cookie by its name
function getCookie(cname) {
let name = cname + "=";
let ca = document.cookie.split(';');
for(let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
// Finding all hidden fields and text input fields in the form
var hiddenFields = document.querySelectorAll("input[type=hidden], input[type=text]");
// Iterating through all found fields
for (var i=0; i < hiddenFields.length; i++) {
var elementor_field_name = hiddenFields[i].name;
var elementor_field_name_clean = elementor_field_name.match(/\[(.*?)\]/);
if(elementor_field_name_clean) {
// Getting the cookie value for the corresponding parameter
var param = getCookie(elementor_field_name_clean[1]);
}
// If a corresponding parameter is found in the cookies, populate the field
if(param) {
document.getElementsByName(hiddenFields[i].name)[0].value = param;
}
}
}
setTimeout(function() {
queryForm();
}, 3000);
})