How to load js file in the header before the body

Hi guys,
I want the page to be loading a js file in a header before the body.
In a script tag in the header I add the async attribute, in the body I don’t have it.
Some time it works and some times it doesn’t.
When it doesn’t work variable in the second js file will be undefined.

The first file contains variables in a global scope (global local variables). The second file is for functions using variables from those defined in the first file.

The reason I do this is to store some data in the memory so it wouldn’t need to load again when I change function. It’s one page website with native app-like functionality.

When user select that javascript functionality (each function is huge) the third or fourth or fifth js file will be loaded into the page and override the existing script dynamically.

There is no error with new script injection, but the initial page load is not quite what I expected.

Thanks

Unless the JavaScript needs to be run before the body of the page loads (in which case you can’t use async) then there are lots of reasons for moving the JavaScript to the bottom of the body instead.

Thank you felgall,

I’ll try and see about it.