简体   繁体   English

Javascript在Chrome中返回null,在源代码中返回HTML元素

[英]Javascript returns null in Chrome, HTML element in source

I am trying to read this HTML in the console in Chrome: 我正在尝试在Chrome的控制台中阅读以下HTML:

<span id="lblSummaryFreight">Kr 79</span>

Using this JS: 使用此JS:

document.getElementById('lblSummaryFreight').innerHTML;

However, after the page has loaded, running this line returns null or invalid. 但是,在页面加载后,运行此行将返回null或无效。 If i inspect the element and then run the code, it works as intended and returns "Kr 79". 如果我检查该元素,然后运行代码,它将按预期工作并返回“ Kr 79”。 So, is it some kind of DOM issue I am not aware of, or a browser-specific issue? 那么,是我不知道的某种DOM问题,还是特定于浏览器的问题?

I am using this as a variable in Google Tag Manager, and it works in 50% of the cases. 我在Google跟踪代码管理器中将其用作变量,并且在50%的情况下均可使用。 I don't have access to the source code of the webpage itself, so that's why I need to lean on this rather clunky way of getting the data. 我无权访问网页本身的源代码,因此这就是为什么我需要依靠这种笨拙的方式获取数据的原因。

A lot of posts on this suggest that this is because the script is fired before the DOM is ready, but I don't think this is an issue, as I am also testing this in the console after the page has loaded (and the HTML elements are present), and in Google Tag Manager I have specified that the tag should fire after DOM is ready. 关于这一点的许多文章表明,这是因为脚本在DOM准备就绪之前就被触发了,但是我认为这不是问题,因为我也在页面加载后在控制台中对此进行了测试(以及HTML元素),并且在Google跟踪代码管理器中,我指定了在DOM准备就绪后应触发代码。

Any clues? 有什么线索吗?

Edit / Clarification: I can't alter the code of the page itself, only read the output source, which i am trying with JS via GTM 编辑/说明:我无法更改页面本身的代码,只能读取输出源,我正在通过GTM与JS进行尝试

Don't seem to be able to resolve this issue, and as it seems to be the fault of some quirky source, I will try to figure out another way to get the data I need. 似乎无法解决此问题,并且似乎是某些古怪来源的过错,因此我将尝试找出另一种获取所需数据的方法。

What I have learned: 我学到:

  • The issue seems to be specific to Chrome, as it works in other browsers. 该问题似乎特定于Chrome,因为它可以在其他浏览器中使用。 This is supported by the fact that the GTM tag where this code is implemented returns correct values in ~50% of the cases 实施此代码的GTM代码在大约50%的情况下会返回正确的值,这证明了这一点。
  • Testing the page in Android native browser, it will also return 'undefined'. 在Android本机浏览器中测试该页面,它还将返回“ undefined”。 After reloading the page that fires the tag, it returns correct value. 重新加载触发代码的页面后,它会返回正确的值。
  • The whole DOM seems unavailable in the console. 整个DOM在控制台中似乎不可用。 Tried also this: 也尝试过这个:

    document.getElementsByClassName('complete').length document.getElementsByClassName('complete')。length

Which returns 0, but there are around 10 instances of the class in the source. 它返回0,但是在源中大约有10个该类的实例。 After inspecting anywhere on the page, it returns correct number. 检查页面上的任何位置后,它将返回正确的数字。

  • Any delay in running the script won't help, only the symbolic inspecting of elements or reloading the page helps. 运行脚本的任何延迟都无济于事,只有符号检查元素或重新加载页面才有帮助。

So my conclusion is that the way this webpage is built somehow goes against the grain of some browsers - it seems like the source goes out of the memory after the source is loaded. 因此,我的结论是,构建此网页的方式在某种程度上与某些浏览器背道而驰-似乎在加载源之后,该源就会耗尽内存。 But this is way beyond my level of understanding. 但这超出了我的理解水平。

Thanks all for all inputs! 感谢所有的投入!

It looks like an async issue. 看起来是一个异步问题。 Try this: 尝试这个:

//Using Jquery
$( document ).ready(function() {
    document.getElementById('lblSummaryFreight').innerHTML;
});

or 要么

//Add this at the end of the body, after all of your content
<script>
(function() {
     document.getElementById('lblSummaryFreight').innerHTML;
})();
</script>

Try to change how the tag is fired in GTM. 尝试更改在GTM中触发代码的方式。 Fire them when Window Loaded, and try again. 在“窗口加载”时将其触发,然后重试。 Maybe the element is being changed in another js file. 也许在另一个js文件中更改了元素。

Trying jQuery is a good point. 尝试使用jQuery是一个好方法。 I have had similar problems and jQuery have solved them all. 我也遇到过类似的问题,jQuery已解决了所有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM