Call a function before loading | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

3rd Dec 2018, 12:51 PM
Paul Hirschberger
Paul Hirschberger - avatar
1 Answer
+ 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
3rd Dec 2018, 5:12 PM
Gordon
Gordon - avatar