I have the following DOM element changing function using plain JavaScript, in which I use the page load event to change a span element containing some text string of the DOM.
With the following code, the DOM elements will change as expected. However, variables desktop
and mobile
will still see the minimal portion of the flickering before their DOM elements change.
There are many reasons why it might flicker, but there are two precautions you can take:
defer
on script tags<script defer>
as this allows the browser to handle the order in which scripts are run instead of usingDOMContentLoaded
. You can also avoidchanges
wrapper functions.In any case, you need to wait until the DOM is loaded before you can operate on it. Using an even listener with
DOMContentLoaded
would be the way to go. So, three things need to happen:visibility:hidden
ordisplay:none
. The difference is that