【问题标题】:Function is not called - tried JavaScript and JQuery未调用函数 - 尝试了 JavaScript 和 JQuery
【发布时间】:2012-09-26 19:06:59
【问题描述】:

我正在尝试从 Google Analytics 标记中删除特殊字符。我有以下脚本,但我不知道为什么它不起作用。它是 JSP 和 JavaScript 的混合体,我对此知之甚少!在工作中学习...

                <script type="text/javascript">
                function removeSplChars(inStr) {
                inStr = inStr.replace(/[^a-zA-Z0-9 ]/g, "");
                return inStr;
                }
                var _gaq = _gaq || [];
                _gaq.push(['_setAccount', '<c:out value="${profileId}"/>']);
                <c:choose>
                <c:when test="${(lastCmdName eq 'CategoryDisplay') or (lastCmdName eq 'ProductDisplay')}" >
                _gaq.push(['_setCustomVar',
                2, // This custom var is set to slot #2.
                '<c:choose><c:when test="${WCParam.source eq 'search'}">Search</c:when><c:otherwise><c:out value="${topCat}" /></c:otherwise></c:choose>', // The top-level name for your online content categories.
                '<c:choose><c:when test="${WCParam.source eq 'search'}">Search <c:out value="${WCParam.searchTerm}" /></c:when><c:otherwise><c:out value="${topCat}" />|<c:out value="${subCatA}" />|<c:out value="${subCatB}" />|<c:out value="${subCatC}" /></c:otherwise></c:choose>', // Records value of breadcrumb name
                3 // Sets the scope to page-level.
                ]); 
                </c:when>
                <c:otherwise>
                </c:otherwise>
                </c:choose>
                removeSplChars(<c:out value="${topCat}" />, <c:out value="${subCatA}" />, <c:out value="${subCatB}" />, <c:out value="${subCatC}" />);
                 _gaq.push(['_trackPageview']);
                (function() {
                var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
                })();
                </script>

我是否遗漏了一些 () 或某处的东西?我得到一个“意外的令牌;”控制台中的错误消息。我尝试将 removeSplChars 调用放在 $(document).ready(function() {});

无济于事。

我检查了以下帖子,但这些建议对我没有帮助: Function is not Called javascript function not getting called

我这里还有一个帖子:JavaScript Remove special characters string not working

谢谢。

通过查看源代码添加代码:

                <script type="text/javascript">
                function removeSplChars(inStr) {
                inStr = inStr.replace(/[^a-zA-Z0-9 ]/g, "");
                return inStr;
                }       
                var _gaq = _gaq || [];
                _gaq.push(['_setAccount', 'UA-33021136-1']);


                _gaq.push(['_setCustomVar',
                2, // This custom var is set to slot #2.
                'Dine &amp; Entertain', // The top-level name for your online content categories.
                'Dine &amp; Entertain|Bowls &amp; Platters||', // Records value of breadcrumb name
                3 // Sets the scope to page-level.
                ]); 



                $(document).ready(function() {
                removeSplChars(Dine &amp; Entertain, Bowls &amp; Platters, , );
                });
                _gaq.push(['_trackPageview']);

                (function() {
                var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
                })();

                </script>

【问题讨论】:

  • 最好在浏览器中查看生成的代码 [aka view source] 而不是服务器端标记。
  • @epascarello,谢谢,我已更新我的帖子以显示该代码。如果您能提供任何帮助,我将不胜感激。

标签: javascript function jsp analytics


【解决方案1】:

传递给removeSplChars 的字符串没有用引号括起来,因此会出现错误。如果可以的话,在对它们进行 JSON 编码后传递变量会更可靠。或者,您可以将" 放在它周围并希望最好(如果您已经在输入中将" 编码为&amp;quot;,它应该可以正常工作)。

此外,这将解决您在使用空参数时遇到的问题:空字符串将作为参数传递。

【讨论】:

  • 谢谢。我添加了引号并且不再收到错误,但我仍然在我的字符串中看到特殊字符。是什么赋予了?这让我发疯了。
  • 你知道为什么现在我添加了引号就不能工作了吗?
猜你喜欢
  • 2014-07-31
  • 2012-04-27
  • 1970-01-01
  • 1970-01-01
  • 2020-04-05
  • 1970-01-01
  • 1970-01-01
  • 2017-01-04
  • 1970-01-01
相关资源
最近更新 更多