It’s easy to measure element size. And I was surprised when I ran my code in Opera and got 0
of the button
height.
I thought by the time, when DOMContentLoaded
event fired, DOM construction had been compliteed and all elements had obtained its sizes.
HTML
<button id="b" class="btn btn-primary" type="button">...</button>
JS
document.addEventListener('DOMContentLoaded', (ev) => {
let b = document.querySelector('#b');
let h = b.offsetHeight; // 37 (Edge), 0 (Opera)
});
This code works fine in Edge browser but Opera seems to have another behavior and doesn't wait for DOM to be created. So I was forced to make artificial delay. But it looks ugly enought.
document.addEventListener('DOMContentLoaded', async (ev) => {
await new Promise((resolve, reject) => {setTimeout(() => resolve(), 200)});
// ...
});
Is there common way to solve suck a prbnlem?
Thanks.