一段时间以来,我一直在收集 UTM 参数并使用这些值成功填充表单字段。utm_source
如果我的为空白,我不想默认为空白或空值,而是为utm_source
“网站”提供默认值
这是我的代码:
// Parse the URL
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
var results = regex.exec(location.search);
return results === null
? ""
: decodeURIComponent(results[1].replace(/\+/g, " "));
}
// Give the URL parameters variable names
var source = getParameterByName("utm_source");
var medium = getParameterByName("utm_medium");
var campaign = getParameterByName("utm_campaign");
// Put the variable names into the hidden fields in the form.
// selector should be "p.YOURFIELDNAME input"
document.querySelector("p.utm_source input").value = source;
document.querySelector("p.utm_medium input").value = medium;
document.querySelector("p.utm_campaign input").value = campaign;
没有错误消息,脚本按预期工作。如果其中一个参数为空白,我只是想添加一个默认值。我怎样才能做到这一点?