js trigger CTA btn

Hello!

I want to implement: when the url has a specific parameter, such as “? from=google”, the CTA form will automatically pop up when the page is opened. I tried using the js native method to trigger the button, but it didn’t work.

code:

window.onload = function() { 
 var hasFromParam = window.location.href.includes('from'); 

 if (hasFromParam) { 

 setTimeout(function() { 

 var btn = document.querySelector('.hs-cta-trigger-button-XXXXXX'); 
 if (btn) { 
 btn.click(); 
 } else { 
 console.error('no XXXX btn'); 
 } 
 }, 3000); 
 } 
};

Hi @xLiu7

Could you please clarify the meaning of the term “pop-up” in the context it was used? Like do you want to disable the button or open a pop-up form on click?

Thanks!

Thanks for the reply! Pop up means that open the pop-up form as you open the page. CTA is added to the page, and when clicked the button using the class of hs-btn directly, the form opens. But when I tried using JS and jQuery trigger buttons, it actually clicked the button, but the form didn’t open.

Hi @xLiu7

You can’t trigger an event by a different event. You can use js to change the visibility of the popup form if the page URL has your slug. Create a function that checks URL slug if that URL has your desired slug then it should change the popup form CSS to display: the block

I hope this will help you out. Please mark it as Solution Accepted and upvote to help another Community member.
Thanks!

open “Trigger after elapsed time” and set time & query parameters.