I am learning Google scripting and I've created a simple web app. Using it (client-side), a creep behavior (to me) was observed in Google Chrome navigator:
Trying to get a element via console with the command document.getElementById("ID of a active Element")
, no element was found. But, after analyze it with a inspector tool (ctrl+c in FireFox and in Chrome, and making sure that such ID exists), the element becomes reachable and, by simply repeating the fetch command ( document.getElementById("ID of a active Element")
), the expected return happens, that means, the function work properly, finds the element. I have no idea why and how such behavior should happen.
Questions: Why that behavior? How could I safely create or avoid it?
System Information: Linux debian 4.9.0-11-amd64 #1 SMP Debian 4.9.189-3+deb9u2 (2019-11-11) x86_64 GNU/Linux
P.S: In FireFox, it always return null, even after inspecting the element! The Chrome behavior is stable (doesn't find before inspection, finds after).
Thanks in advance.
A Link if you want to experience this behavior
Below, the code of the page above. The doGet function only returns that page. There is nothing more than that.
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
body{ display: grid; align-items: center; }
button { padding-left: 40%;padding-right: 40%; }
</style>
</head>
<body>
<h1> Thank you for your support! </h1>
<p> The button below has ID <strong>bbb</strong>. Try to find it in console with the command <span >document.getElementById("bbb")</span>. In Chrome, you will get null. But, after inspect the element (ctrl+c and click over it), you will get the button.</p>
<button id="bbb"> My ID is bbb - click on me to copy the mentioned command above. </button>
<p> If you try <strong>document.body.getElementsByTagName("IFRAME")[0].contentWindow.document.getElementById('bbb')</strong>, you will fail due to lack of permissions</p>
<button id="fff"> My ID is fff - click on me to copy the mentioned command above.</button>
</body>
<script>
//the code of copyToClipboard was copied from https://www.30secondsofcode.org/blog/s/copy-text-to-clipboard-with-javascript only used to illustrate this question.
const copyToClipboard = str => {
const el = document.createElement('textarea');
el.value = str;
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
};
document.getElementsByTagName("BUTTON")[0].onclick = function(){ copyToClipboard( 'document.getElementById("bbb")' )};
document.getElementsByTagName("BUTTON")[1].onclick = function(){ copyToClipboard( 'document.body.getElementsByTagName("IFRAME")[0].contentWindow.document.getElementById("fff")' )};
</script>
</html>