+ 1
Call a function before loading
Hi again ! I have a function to randomize the margin of several elements. I invoke it in my HTML by using the "onload" attribute en <body>. How can I invoke it before the loading of <body> ? thanks ! Paul https://code.sololearn.com/WivGo6fAy4SP/?ref=app
1 Resposta
+ 3
Call the function directly in the JS tab of code playground is equivalent to putting them in head tags, which will be executed before body is loaded.
https://code.sololearn.com/WvphMx27ro0e/?ref=app
Notice that the width are not changed, because if you call this function before body, it will result in all getElementById returning null.
As an additional information, here is a demo about what would happen if you addEventListener before document is loaded.
https://code.sololearn.com/WriFNOoR188j/?ref=app