javascript Google 标签管理器解析错误。主要表达

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32418777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:14:27  来源:igfitidea点击:

Google Tag Manager Parse Error. Primary Expression

javascriptgoogle-tag-manager

提问by Spencer Gallardo

I am trying to save this Custom Javascript Variable within Google Tag Manager and return this error each time:

我试图在 Google 标签管理器中保存这个自定义 Javascript 变量并每次都返回此错误:

Error at line 2, character 4: Parse error. primary expression expected

第 2 行错误,字符 4:解析错误。预期的主要表达

var prodID = function pID() {
return document.querySelector(".no-display > input[name=product]").value;
}();

Can someone help me? I believe I have a primary expression.

有人能帮我吗?我相信我有一个主要的表达。

回答by Spencer Gallardo

To fix the issue in Google tag manger, I simple re wrote the code to be :

为了解决 Google 标签管理器中的问题,我简单地重新编写了代码:

function() {
  return document.querySelector(".no-display > input[name=product]").value;
}

回答by ted_zarzeczny

All code in a custom javascript variable should be within function() {}, or you will get this error.

自定义 javascript 变量中的所有代码都应在 function() {} 内,否则您将收到此错误。

Here's the documentation from Google:

这是谷歌的文档:

This field should be a JavaScript function that returns a value using the 'return' statement. If the function does not explicitly return a value, it will return undefined and your container may not behave as expected. Below is an example of this field:

该字段应该是一个使用“return”语句返回值的 JavaScript 函数。如果函数没有显式返回值,它将返回 undefined 并且您的容器可能不会按预期运行。以下是此字段的示例:

function() {
     var now = new Date();
     return now.getTime();   
}