When displaying a person's name on a page, there are two different methods to consider. It can be extracted from the URL or retrieved from an email form in the CMS used. However, these two approaches sometimes conflict with each other.
Currently, I have a function that successfully extracts the person's name from the "first_name" parameter in the URL. But before doing so, I want it to wait for a few seconds and check if another div is empty. This delay ensures that if the CMS is going to populate that div with the name, it has ample time to do so.
The process involves waiting a few seconds, examining the span element to see if it is empty, and then filling the div with the name fetched from the URL.
This is what the current implementation looks like:
HTML
<div id="firstName"></div> <!--This div will be filled if the other one is empty -->
<span merge-tag="{{lead.first_name}}"></span> <!--Check if this span is empty before populating the div -->
Javascript
// URL Parser
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 'blank';
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
// Extracting First Name from URL
var firstName = getParameterByName('first_name');
// Function to add first name to div if the span with merge-tag="{{lead.first_name}}" is empty.
function addName() {
if ($("[merge-tag={{lead.first_name}}]").is(':empty')) {
document.getElementById("firstName").innerHTML = firstName.toString();
}
}
window.settimeout(addName, 5000);
The current script is not functioning as expected, and I’m seeking suggestions from anyone who might have insights into why it's not working properly.