【问题标题】:Trouble writing cookie value to form field无法将 cookie 值写入表单字段
【发布时间】:2013-10-16 14:46:46
【问题描述】:

作为 Javascript 新手,我在我们的网站上实施 Google 的 Adwords GCLID 跟踪时遇到了一些麻烦。我正在关注他们在此处显示的代码示例https://support.google.com/adwords/answer/2998031

您可以看到正在存储的 cookie,但在尝试检索它并填充隐藏的表单字段时,结果只是空白。我使用了其他变体,但这只会导致“未定义”作为字段值。

这是我正在使用的代码

简化的 HTML 表单

<form class="signup-form" name="signupform" method="post" action="step2.php">
    <input type="hidden" name="gclid" id="gclid" value="" />
</form>

Cookie 编写脚本

<script type="text/javascript">
    function setCookie(a,d,b){var c=new Date;c.setTime(c.getTime()+864E5*b);b=";
    expires="+c.toGMTString();document.cookie=a+"="+d+b}function getParam(a)     {return(a=RegExp("[?&]"+a+"=([^&]*)").exec(window.location.search))&&decodeURIComponent(a[1].replace(/\+/g," "))}var gclid=getParam("gclid");if(gclid){var gclsrc=getParam("gclsrc");(!gclsrc||-1!==gclsrc.indexOf("aw"))&&setCookie("gclid",gclid,90)};
</script>

Cookie 读取脚本

<script> 
  function readCookie(name) { 
      var n = name + "="; 
      var cookie = document.cookie.split(';'); 
      for(var i=0;i < cookie.length;i++) {      
          var c = cookie[i];      
          while (c.charAt(0)==' ') {
            c = c.substring(1,c.length);
          }      
          if (c.indexOf(n) == 0){
            return c.substring(n.length,c.length);
          } 
      } 
      return null; 
  } 

  function() {      
      document.getElementById('gclid').value = readCookie('gclid'); 
  } 
</script>

【问题讨论】:

    标签: javascript html cookies google-ads-api


    【解决方案1】:
    function() {      
        document.getElementById('gclid').value = readCookie('gclid'); 
    } 
    

    这是一个从未调用过的没有名字的函数。尝试只替换它

    document.getElementById('gclid').value = readCookie('gclid'); 
    

    我重写了你的写脚本,我认为你的 cookie 从未设置过。

    function setCookie(a,d,b) {
        var c = new Date;
        c.setTime(c.getTime()+864E5*b);
        b="; expires=" + c.toGMTString();
        document.cookie = a + "=" + d + b
    }
    
    function getParam(a) {
        return(a=RegExp("[?&]"+a+"=([^&]*)").exec(window.location.search))&&decodeURIComponent(a[1].replace(/\+/g," "))
    }
    
    var gclid=getParam("gclid");
    
    if(gclid) {
        var gclsrc = getParam("gclsrc");
        if(!gclsrc||-1 !== gclsrc.indexOf("aw")) {
            setCookie("gclid", gclid, 90);
            alert("Cookie Set!");
        }
    }
    

    【讨论】:

    • 嗯,这是朝着正确方向迈出的一步,但现在它在该表单字段值中返回一个值“未定义”。测试页面已随此更改而更新。
    • 我添加了你重写的写脚本,请测试你的代码是否提示“Cookie Set!”。
    • 它确实会提醒“Cookie Set”,但仍会返回“未定义”。代码检查器显示: 似乎也没有任何控制台错误。
    • 那你的read函数肯定有问题,试试MDN提供的框架developer.mozilla.org/en-US/docs/Web/API/document.cookie
    • 感谢您的帮助。实际上,我使用另一个专门针对 Salesforce 的示例代码自己解决了这个问题。原来这是一个加载问题。该脚本在表单之前加载时执行。
    【解决方案2】:

    https://support.google.com/adwords/answer/3285060 修改的工作脚本。我刚刚将 document.onload 更改为 window.onload 似乎 DOM 中的屏幕外发生了太多事情。

    <script> 
     window.onload = function getGclid() {        
       document.getElementById("gclid").value = (name = new    
     RegExp('(?:^|;\\s*)gclid=([^;]*)').exec(document.cookie)) ? 
     name.split(",")[1] : ""; } 
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 1970-01-01
      • 2018-09-07
      • 2021-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多