JavaScript Where to
2 April 2025 | Category: JavaScript
JavaScript can be added to a webpage in three different ways:
- Inline JavaScript (Inside HTML elements)
- Internal JavaScript (Inside the
<script>
tag in an HTML file) - External JavaScript (Using a separate
.js
file)
Let’s explore each method in detail! 👇
1️⃣ Inline JavaScript (Inside HTML Elements)
If you want to add a small script, you can write JavaScript directly inside an HTML tag using event attributes like onclick
.
🔹 Example:
<button onclick="alert('Button Clicked!')">Click Me</button>
✅ When to Use?
✔️ For very small scripts
✔️ When you need to execute JavaScript for just one element
❌ When to Avoid?
❌ If the script is too large, it makes the HTML messy
❌ It is hard to reuse the script elsewhere
2️⃣ Internal JavaScript (Inside the <script>
Tag in an HTML File)
If you need to write more JavaScript, you can include it inside an HTML file using the <script>
tag.
🔹 Example:
<!DOCTYPE html>
<html>
<head>
<title>Internal JavaScript</title>
</head>
<body>
<h1>Welcome to JavaScript</h1>
<script>
alert("Hello, JavaScript!");
console.log("This is an internal script.");
</script>
</body>
</html>
✅ When to Use?
✔️ If the JavaScript only applies to one webpage
✔️ For small to medium scripts
❌ When to Avoid?
❌ If the script is too large or needs to be used on multiple pages
3️⃣ External JavaScript (Using a Separate .js
File)
If you want to keep your JavaScript separate and reuse it across multiple pages, the best practice is to use an external JavaScript file.
🔹 Step 1: Create a .js
file (e.g., script.js
)
console.log("Hello from an external JavaScript file!");
🔹 Step 2: Link the file inside the HTML file using <script>
<!DOCTYPE html>
<html>
<head>
<title>External JavaScript</title>
<script src="script.js"></script>
</head>
<body>
<h1>External JavaScript File</h1>
</body>
</html>
✅ When to Use?
✔️ If your script is large or needs to be reused across multiple pages
✔️ For better code organization and maintenance
❌ When to Avoid?
❌ If the script is very small, creating a separate file might not be necessary
💡 Where to Place JavaScript Code? (Best Practices)
1️⃣ Don’t place <script>
inside <head>
⚠️
<head>
<script src="script.js"></script>
</head>
❌ Problem: JavaScript may load before the HTML elements are created, causing errors.
2️⃣ Best practice: Place <script>
at the end of <body>
✅
<body>
<h1>Hello, JavaScript!</h1>
<script src="script.js"></script>
</body>
✅ This ensures HTML loads first, then JavaScript runs, making the page faster.
3️⃣ Use the defer
attribute ✅
<script src="script.js" defer></script>
✅ defer
makes sure JavaScript only runs after the full HTML is loaded.
🔍 Summary: Different Ways to Use JavaScript
Method | Example | When to Use? |
---|---|---|
Inline JavaScript | <button onclick="alert('Hello!')">Click</button> | For small scripts |
Internal JavaScript | <script> alert("Hello!"); </script> | For a single webpage |
External JavaScript | <script src="script.js"></script> | For reusable code across multiple pages |
🚀 Conclusion
- If you’re writing a small script, use Inline JavaScript.
- If your script is medium-sized and applies to a single page, use Internal JavaScript.
- If your script is large or used on multiple pages, External JavaScript is the best choice! 🎯