Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript returns null in Chrome, HTML element in source

I am trying to read this HTML in the console in Chrome:

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

Using this JS:

document.getElementById('lblSummaryFreight').innerHTML;

However, after the page has loaded, running this line returns null or invalid. If i inspect the element and then run the code, it works as intended and returns "Kr 79". So, is it some kind of DOM issue I am not aware of, or a browser-specific issue?

I am using this as a variable in Google Tag Manager, and it works in 50% of the cases. 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.

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

like image 666
Mikken Avatar asked Aug 11 '16 10:08

Mikken


People also ask

Why is my HTML element null?

This can happen if the JavaScript code is executed before the page is fully loaded, so its not able to find the element. The solution is that you need to put your JavaScript code after the closure of the HTML element or more generally before < /body > tag.

Why is element null JavaScript?

The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy for boolean operations.

Why is get element by id returning NULL?

The document. getElementById() method returns null in React when we call the method before the element with the provided ID has been rendered to the DOM or if an element with the ID doesn't exist. To get around this, call the getElementById() method in the useEffect hook.

Can a JavaScript function return null?

JavaScript uses the null value to represent the intentional absence of any object value. If you find a variable or a function that returns null , it means that the expected object couldn't be created.


1 Answers

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. This is supported by the fact that the GTM tag where this code is implemented returns correct values in ~50% of the cases
  • Testing the page in Android native browser, it will also return 'undefined'. After reloading the page that fires the tag, it returns correct value.
  • The whole DOM seems unavailable in the console. Tried also this:

    document.getElementsByClassName('complete').length

Which returns 0, but there are around 10 instances of the class in the source. 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!

like image 137
Mikken Avatar answered Oct 30 '22 08:10

Mikken