【问题标题】:How do I populate a default value for a UTM parameter when a value is undefined?当值未定义时,如何填充 UTM 参数的默认值?
【发布时间】:2019-10-05 01:56:18
【问题描述】:

一段时间以来,我一直在收集 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;

没有错误消息,脚本按预期工作。如果其中一个参数为空白,我只是想添加一个默认值。我该怎么做?

【问题讨论】:

    标签: javascript forms utm pardot


    【解决方案1】:

    假设您的 getParameterByName 函数在没有找到任何内容时返回 undefined 或类似的虚假值,您应该能够通过条件来做到这一点:

    var source = getParameterByName('utm_source') || 'my default value';
    

    【讨论】:

    • 谢谢。我已经使用条件值运行了一些测试,它仍然将未知 utms 的字段值留空。捕获列出的那些,但不捕获空的或缺失的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多