Custom code does not fire for single page application

gerard_reyes Posted by: Gerard Reyes Date Posted: February 17, 2025
Back to Case Studies

Issue

Initially, the form is not firing the RCFX lead capture function. I added a custom code to fir the RCFX lead capture.

However, my code fires on site load. But the client’s site does not reload on navigating of site but rather loads asynchronously. The result of this is my code is not firing for the new page navigated to.

Example, if a user moved from the Contact Us page to the Get Quote page. My custom code only fires for the Contact Us page and not on the Get Quote page.

Solution

I modified the code to fire regardless of the navigation of the site.


// Function to assign custom names to inputs without names
function assignCustomNamesToInputs() {
var inputs = document.querySelectorAll('input, textarea');
var counter = 0;
for (var i = 0; i < inputs.length; i++) { var input = inputs[i]; if (!input.name && input.id) { input.name = input.id.split('-:')[0]; counter++; } } }


// Call the function to execute it
setInterval(function() {
assignCustomNamesToInputs();
}, 1000); // 1000 milliseconds = 1 second

Achievement

The custom code is firing and captures the lead.

Snippet/Screenshot/Link

Task: Link
Code: Screenshot