Industry-relevant training in Business, Technology, and Design to help professionals and graduates upskill for real-world careers.
Fun, engaging games to boost memory, math fluency, typing speed, and English skills—perfect for learners of all ages.
Enroll to start learning
You’ve not yet enrolled in this course. Please enroll for free to listen to audio lessons, classroom podcasts and take mock test.
Listen to a student-teacher conversation explaining the topic in a relatable way.
Signup and Enroll to the course for listening the Audio Lesson
Today, we'll explore how to embed JavaScript in our HTML files. This is done using the `<script>` tag. Can anyone tell me what this tag does?
Does it allow us to write JavaScript code?
"Exactly! Here's an example:
Signup and Enroll to the course for listening the Audio Lesson
Now let's talk about external JavaScript files. This is done by linking a separate .js file using `<script src='script.js'></script>`. Why do we want to do this?
To keep our HTML clean and organized!
Exactly! Plus, what are some benefits of keeping JavaScript in a separate file?
We can reuse the same script across multiple HTML pages!
And it's easier to manage changes!
Great insights! By maintaining separate files, we not only promote organized code but also enhance collaboration in larger projects.
That makes a lot of sense!
Read a summary of the section's main ideas. Choose from Basic, Medium, or Detailed.
In this section, we explore two key methods to use JavaScript in webpages: embedding it directly within HTML using the
JavaScript can enhance user interactivity on webpages, and this section focuses on two primary ways to include JavaScript code:
<script>
tags. For example:Hidden Test Case
${codeResult ? atob(codeResult.stdin) : "No input available"}
${codeResult ? atob(codeResult.expected_output) : "No expected output available"}
${codeResult ? (codeResult.stderr || codeResult.compile_output ? atob(codeResult.stderr || codeResult.compile_output) : codeResult.stdout == null ? "" : atob(codeResult.stdout)) : "No output available"}
This method is straightforward but can cause clutter in larger documents.
script.js
) and link it to your HTML document.Hidden Test Case
${codeResult ? atob(codeResult.stdin) : "No input available"}
${codeResult ? atob(codeResult.expected_output) : "No expected output available"}
${codeResult ? (codeResult.stderr || codeResult.compile_output ? atob(codeResult.stderr || codeResult.compile_output) : codeResult.stdout == null ? "" : atob(codeResult.stdout)) : "No output available"}
Using external files is generally better for large projects as it keeps the code organized and separates content from behavior. The linking method promotes clean code structure and reusability across different HTML files.
Dive deep into the subject with an immersive audiobook experience.
Signup and Enroll to the course for listening the Audio Book
You can write JavaScript directly inside an HTML file using the <script>
tag:
Hidden Test Case
${codeResult ? atob(codeResult.stdin) : "No input available"}
${codeResult ? atob(codeResult.expected_output) : "No expected output available"}
${codeResult ? (codeResult.stderr || codeResult.compile_output ? atob(codeResult.stderr || codeResult.compile_output) : codeResult.stdout == null ? "" : atob(codeResult.stdout)) : "No output available"}
Internal JavaScript is used when you want to include JavaScript code directly within your HTML file. This is done using the <script>
tag. When the HTML page is loaded, the browser will execute the JavaScript code inside the <script>
tag. For example, in the provided code, when a user visits the webpage, an alert box pops up displaying the message "Hello, welcome to JavaScript!"
Think of internal JavaScript like notes written directly in a recipe book. When you open the book to read the recipe, you can see the notes right there. Similarly, when a browser opens an HTML file, it sees the JavaScript code immediately and runs it without needing to look elsewhere.
Signup and Enroll to the course for listening the Audio Book
You can also write JavaScript in a separate .js
file and link it in your HTML:
HTML:
Hidden Test Case
${codeResult ? atob(codeResult.stdin) : "No input available"}
${codeResult ? atob(codeResult.expected_output) : "No expected output available"}
${codeResult ? (codeResult.stderr || codeResult.compile_output ? atob(codeResult.stderr || codeResult.compile_output) : codeResult.stdout == null ? "" : atob(codeResult.stdout)) : "No output available"}
script.js:
Using external JavaScript files is particularly beneficial for larger web projects. This means you can keep your JavaScript code separate from your HTML, which makes your code cleaner and easier to manage. To link an external JavaScript file, you use the <script>
tag with the src
attribute pointing to the JavaScript file. In the example, when the webpage loads, it will execute the JavaScript code in the script.js
file, showing an alert that says "JavaScript file loaded!".
Imagine you have a library where all books (JavaScript code) are stored separately, rather than scattered throughout your home (HTML file). You can just refer to the library and pull out any book you need, making it easier to find and update your information without cluttering your living space.
Signup and Enroll to the course for listening the Audio Book
🟡 Using external files is better for large projects.
When working on larger projects, separating JavaScript code into external files helps keep the code organized. It allows multiple HTML files to reference the same JavaScript file without duplicating the code, making it more efficient. Any changes made to the external JavaScript file will automatically update across all HTML files using it, thus simplifying maintenance and updates.
Consider a car factory where each model has shared parts. If all models use the same engine design stored in one location, it is easy to improve or fix. This is like having an external JavaScript file: you can enhance one piece of code, and all projects that use it benefit from the upgrades without needing to change every single car model.
Learn essential terms and foundational ideas that form the basis of the topic.
Key Concepts
Internal JavaScript: Code that is written inside HTML documents using the
External JavaScript: A design choice to write JavaScript in external files that are linked to HTML documents.
See how the concepts apply in real-world scenarios to understand their practical implications.
Internal JavaScript example that uses an alert box to greet users.
An HTML document that links to an external JavaScript file named script.js.
Use mnemonics, acronyms, or visual cues to help remember key information more easily.
If you want your web page to be peppy, include JavaScript; it makes it zippy.
Imagine you are a chef. If you put all your ingredients in one dish (internal), it can get messy. But when you organize them in the fridge (external files), everything is tidy and fresh!
Remember: JS in HTML = 'Julia Sees Internal' for internal and 'Harry's Home' for external where H links to html.
Review key concepts with flashcards.
Review the Definitions for terms.
Term: Internal JavaScript
Definition:
JavaScript code written directly within an HTML document using the
Term: External JavaScript file
Definition:
A separate JavaScript file that can be linked to an HTML document for better organization.
Term: <script>
Definition:
An HTML tag used to include JavaScript code in HTML documents.