簡體   English   中英

谷歌標簽管理器沒有引用 jQuery? “$ 不是函數”

[英]Google Tag Manager not referencing jQuery? "$ is not a function"

首先要說我對 JS 和 jQuery 的經驗非常有限,所以我在這里有點猶豫。

我有一個 GTM 自定義 HTML 標簽,它使用 jQuery 從與特定錯誤消息 class 關聯的標簽/圖例元素中獲取文本,並將每個標簽推送到 dataLayer 以進行事件處理:

 <script type="text/javascript" id="error-push"> jQuery( document ).ready(function() { var errMsgs = document.getElementsByClassName("error_message"); for (i = 0; i < errMsgs.length; i++) { var assocLabel = $(errMsgs[i]).closest(".field_error").find(".field_label").text(); if(assocLabel) { dataLayer.push({'field-error': assocLabel}); } } }); </script>

在我的瀏覽器控制台中直接測試時,我可以讓 function 運行沒有問題。 但是當我通過 GTM 運行它時,我得到了一個 Uncaught TypeError:

Uncaught TypeError: $ is not a function
    <anonymous> https://example.com/form/?gtm_debug=1661509869380 line 348 > injectedScript:1
    jQuery 2
        e
        t
register:1:218

起初我以為 GTM 只是在加載 jQuery 之前觸發了腳本,所以我在 GTM 的腳本頂部添加了一個額外的行來檢查它:

<script type="text/javascript" id="error-push">
  typeof window.jQuery !== 'undefined' ? console.log(window.jQuery.fn.jquery) : 'jQuery not found!'
  jQuery( document ).ready(function() { ...

...並且控制台返回“3.6.0”,表明 function 運行加載了 jQuery。 那么 TypeError 是從哪里來的呢?

我個人對 Google Tag Manger 的做法是沒有在其中使用 jQuery。

因為它會有很多錯誤,不知道為什么會出現。

在你的情況下:

我建議你使用觸發器Dom Ready 據我所知,當你想觸發jQuery( document ).ready(function() {}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM