Inline, Internal, and External Scripts
What’s the Difference?
- Inline: JavaScript code is written directly in an HTML tag’s attribute.
- Internal: JavaScript code is placed inside a
<script>
tag in the HTML file.
- External: JavaScript code is in a separate file and linked to the HTML.
Example Table
Type |
Where? |
Example |
Inline |
HTML attribute |
<button onclick=”alert(‘Hi!’)”>Click</button> |
Internal |
<script> tag in HTML |
<script>alert(‘Hi!’);</script> |
External |
Separate .js file |
<script src=”main.js”></script> |
Why Use Each?
- Inline: Quick demos, but not recommended for big projects.
- Internal: Good for small scripts on a single page.
- External: Best for code reuse and organization.