0

我正在尝试在下面的 HTML 中收集值(显示 1 -10 of 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 函数

/** * 在页面加载后返回“Showing 1 -10 of 19300”的文本并将值传递给 dataLayer。*/

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

window.onload = showing;

并传递给 dataLayer 对象(搜索结果),以便谷歌分析可以从 dataLayer 中获取值。

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

  <!-- end dataLayer -->

但是我似乎无法将 querySelector 的值传递给 dataLayer,因为我不断得到“未定义”。是 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?

4

1 回答 1

0

showResult在外部设置datalayer会使程序难以阅读和调试。我的建议是为如下所示定义一个加载器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();
}
于 2016-09-14T00:06:59.003 回答