javascript 仅在URL参数存在时执行脚本

6yoyoihd  于 2022-11-27  发布在  Java
关注(0)|答案(1)|浏览(150)

是否有办法使此脚本仅在URL中存在utm_campaign参数时执行?

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();
}
0g0grzrc

0g0grzrc1#

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
}

相关问题