nodejsafrica / workshoppers

Zero to Hero with Node.js Africa Code Guide for JavaScript and Node.js
MIT License
17 stars 15 forks source link

Script placement in Html?? #6

Closed ifiokabasibom closed 6 years ago

ifiokabasibom commented 6 years ago

Where exactly is the best place to put your JavaScript in your html code? cos I've seen some article that says you should put it in the head if you want the script to run on some event such as a button click else it should be in the body, and even in the body some say it should just before so the page can load faster. I'm really confused atm. And also why is it that sometimes the script is saved as an external file and referenced externally in the head like it's done with external css?? please someone should explain these things so I don't end up confusing myself the more

codeekage commented 6 years ago

Yea, most times the <script></script> is placed in the head, probably you want to run a function or do something before the entire page loads, and it's good practice to place to the <script></script> in the <body></body> that way, it loads faster and elements you've probably selected to work with must have been loaded before executing your scripts.

External Script 'Is actually best practice, as it keeps your HTML neat and imagine if you have 200 lines of js-script, it will be horrible to write all those scripts in your HTML file. Just like you do with CSS, it's always better to put it in a different file and reference it.

ifiokabasibom commented 6 years ago

alright, thanks for the help. i appreciate

On Sat, May 12, 2018 at 5:52 PM, Agiri Abraham Jr notifications@github.com wrote:

Yea, most times the is placed in the head, probably you want to run a function or do something before the entire page loads, and it's good practice to place to the in the that way, it loads faster and elements you've probably selected to work with must have been loaded before executing your scripts.

External Script 'Is actually best practice, as it keeps your HTML neat and imagine if you have 200 lines of js-script, it will be horrible to write all those scripts in your HTML file. Just like you do with CSS, it's always better to put it in a different file and reference it.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/NodeJSAfrica/workshoppers/issues/6#issuecomment-388568134, or mute the thread https://github.com/notifications/unsubscribe-auth/AeQsRMTycWnfqe8CCWyd3zboC4om9Rk1ks5txxM7gaJpZM4T8cJh .