无法使用 javascript 将值从函数传递到数据层数组

Unable to pass a value from a function to a data layer array using javascript

我正在尝试在下面 HTML

中收集值(显示 1 -10,共 74 个)
<body>
<p data-brackets-id="1217" class="search">

                <!--No Results Found-->
                <small data-brackets-id="1218" data-bind="visible:(searchResults().length == 0), html: translations.noResults" style="display: none;">No Results To Display Please Enter A Search Term Above</small>

                <!--Error Message-->
                <small data-brackets-id="1219" class="error-message" data-bind="visible:errorMessage, html:errorMessage" style="display: none;"></small>

                <!--Search Result Count-->
                <small data-brackets-id="1220" data-bind="html:currentlyShowing, visible:(searchResults().length > 0)">Showing 1 -10 of 19300</small>
            </p>
</body>

通过使用 window.onload 函数

/** * returns 页面加载后 "Showing 1 -10 of 19300" 的文本并将值传递给数据层。 */

function showing () {
    var showResults = document.querySelector(".search small :nth-child(3)").innerHTML;
}

window.onload = showing;

并传入数据层对象(搜索结果),以便 google 分析可以从数据层获取值。

<!-- start dataLayer -->
<script>
    var adobeInfo = {
            pageInfo: {
                'pageType': "welcome page",
                'Search Results': showResults
            }
    }

  <!-- end dataLayer -->

但是我似乎无法将 querySelector 的值传递给 dataLayer,因为我一直在获取 "undefined"。是 windows.onload 不允许我传递值,还是我只是使用了错误的代码语法来完成此任务?

提前感谢您的帮助。

<body>
<p data-brackets-id="1217" class="leader-1 no-trailer">
<!--No Results Found-->
<small data-brackets-id="1218" data-bind="visible:(searchResults().length == 0), html: translations.noResults" style="display: none;">No Results To Display Please Enter A Search Term Above</small>

<!--Error Message-->
<small data-brackets-id="1219" class="error-message" data-bind="visible:errorMessage, html:errorMessage" style="display: none;"></small>

<!--Search Result Count-->
<small data-brackets-id="1220" data-bind="html:currentlyShowing, visible:(searchResults().length > 0)">Showing 1 -10 of 74</small>
</p>

<script>  
function results () {
    alert(" alert inside results function");
    var showResults = document.querySelector("leader-1.no-trailer :nth-child(3)").innerHTML; // get the results of the showing info from search page.
    showResults.select();
}

window.onload = results();
</script>

  
<!-- Use dataLayer object to pass values to analytics -->

<script>
    var dataLayer = {
            pageInfo: {
                'pageType': "welcome page",
                'show Results': showResults // value of show results should go here (Showing 1 -10 of 74)
            }
         }
</script>

</body>

所以我想我真正的问题是如何将 window.onload 函数的值传递给全局变量,以便我可以将它传递给我的数据层?

datalayer 之外设置 showResult 会使程序难以阅读和调试。我的建议是为 data layer 定义一个加载程序,如下所示:

var dataLayer = {
  pageInfo: {
    'pageType': "welcome page",
  },
  load: function() {
    this.showResults = document.querySelector("leader-1.no-trailer :nth-child(3)").innerHTML; // get the results of the showing info from search page.
    this.showResults.select();
  }
}


// windows onload event
windows.onload = function() {
  dataLayer.load();
}