只有URL参数存在时才执行脚本



是否有办法使这个脚本只执行,如果一个utm_campaign参数存在于URL?

function fillFormArea(){

const select = document.getElementById('property.cust_AreaOfInterest.value'); 
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);  //parse the query string
const country = urlParams.get('utm_campaign');  // Store the parsed area of interest
select.value = country;  // fill the form with the area of interest
}
if (document.readyState === 'loading') {  // Loading hasn't finished yet
document.addEventListener('DOMContentLoaded', fillFormArea)
} 
else {  // `DOMContentLoaded` has already fired
fillFormArea();
}

const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const country = urlParams.get('utm_campaign');
if ( country === null) {
//does not exist
}else{
const select = document.getElementById('property.cust_AreaOfInterest.value');
select.value = country
}

最新更新