Skip to content Skip to sidebar Skip to footer

How To Call Js Function Before Html Load Completed?

I write a JS function to hide some elements in HTML and use window.onload() to call this function. But there is a problem that window.onload() call functions after HTML load comple

Solution 1:

How about you make the default visibility none first and then enable the ones you want to display after page load?

Solution 2:

I'd suggest that if you want to hide the elements in the first place on page load, you do it directly (either by external or on the fly).

E.g.

<div id="excelDataTable_epgd" style="display: none"> </div>
<div id="EPGD_SIZE" style="display: none"> </div>

Then, on page finishes loading, you can run your JS function to make them visible.

functiontoggle_visibility(id) {
    var e = document.getElementById(id);
    var display = e.style.display;
    if (display == 'none') {
       e.style.display = 'block';
    }
    else {
       e.style.display = 'none';
    }
}

window.onload = function () {
    toggle_visibility("excelDataTable_epgd");
    toggle_visibility("EPGD_SIZE");
}

Solution 3:

As already mentioned, the best way would be to initially hide the element via CSS, since that wouldn't cause the browser to redraw.

But more generally speaking, the fastest way to manipulate a DOM element via JS is to put the code directly after the element.

E.g.

<divid="foo">...</div><script>toggle_visibility('foo')</script>

Solution 4:

Before the HTML load completed, HTML elements have not yet joined the DOM tree, call JS function is unable to operate a DOM element, so you need to use CSS to hide the elements, and then call JS function makes the element display.

Post a Comment for "How To Call Js Function Before Html Load Completed?"