
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
When should I use an Inline script and when to use external JavaScript file?
To enhance performance, try to keep JavaScript external. The separate code makes it easier for web browsers to cache. However, use inline scripts only when you’re making single page websites.
Still, it's better to use external code i.e. external JavaScript. To place JavaScript in external file create an external JavaScript file with the extension .js. After creating, add it to the HTML file in the script tag. The src attribute is used to include that external JavaScript file.
If you have more than one external JavaScript file, then add it to the same web page to increase the performance of the page.
Let’s say the following new.js is our external JavaScript file −
function display() { alert("Hello World!"); }
Now add the external JavaScript file to the HTML web page
Example
Live Demo<html> <body> <form> <input type = "button" value = "Result" onclick = "display()"/> </form> <script src = "new.js"> </script> </body> </html>
- Related Articles
- When to use an inline function in Kotlin?
- When to use inline function and when not to use it in C/C++?
- When Should I use Selenium Grid?
- When should I use MySQL compressed protocol?
- When should you use sets in Javascript?
- When should I use a semicolon after curly braces in JavaScript?
- When you should not use JavaScript Arrow Functions?
- When to use an abstract class and when to use an interface in Java?
- When should I use a composite index in MySQL?
- What are the differences between inline JavaScript and External file?
- When to Use an Alias vs Script vs a New Function in Bash
- How to use external “.js” files in an HTML file?
- When should I use the keyword ‘this’ in a Java class?
- When to use anonymous JavaScript functions?
- What is a smart pointer and when should I use it in C++?

Advertisements