Hey there, web explorers! We’re looking at where to put JavaScript in your HTML. By the end of this video, you’ll know exactly where to place your scripts for maximum efficiency and flexibility.
Let’s get coding! First things first. JavaScript lives between <script> tags.
That’s like its home. Let’s start with an example! This simple script grabs an element with the ID of "demo" and changes its content.
Old examples might include type="text/javascript" but you don’t need that anymore. It’s like wearing socks with sandals. Just don’t.
So, where should you place your scripts? You can put them in the <head> or <body>, or even both! Let's take a look at <head> This works, but scripts in the <head> run before the page content loads.
This might slow the rest of the page down. Let's try <body> When the script’s in the <body>, it runs as the page loads. Making things snappier for users!
Want to take things to the next level? External JavaScript is where it’s at! It’s like having a toolbox you can reuse on every page.
Using external scripts keeps your code neat, reusable, and faster, thanks to browser caching! You can reference external scripts in different ways. Full URLs, relative paths, or no path at all!
The choice depends on your setup. Just remember: no <script> tags in your JavaScript files! So there you have it!
Scripts in the <head> or <body> Inline or external. It’s all about what works for your project. Thanks for watching!
Like and subscribe to get notified about our latest videos! For more coding tips and tricks, keep it locked right here on W3Schools. com!
Don't forget to try out what you've learned in our interactive editor. Happy coding!