
- 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
How to include an external JavaScript inside an HTML page?
The HTML <script> tag is used for declaring a script within your HTML document. Through this, you can define client-side JavaScript. But, what if you want to add external JavaScript inside an HTML Page? Well, you can easily do that too using the src attribute of the <script> tag.
The following are the attributes of the <script> tag −
Attribute | Value | Description |
---|---|---|
async | async | Specifies that the script is executed asynchronously. |
charset | charset | Defines the character encoding that the script uses. |
defer | defer | Declares that the script will not generate any content. Therefore, the browser/user agent can continue parsing and rendering the rest of the page. |
src | URL | Specifies a URI/URL of an external script. |
type | text/JavaScript application/ecmascript application/JavaScript text/vbscript | Specifies the scripting language as a content-type (MIME type). |
For adding external JavaScript file, we will be using the src attribute −
Let’s see the HTML file first, which will include the source for the external file source.
Example
You can try to run the following code to include external JS in HTML. The HTML file is here, with a src to the JS file myscript.js.
<!DOCTYPE html> <html> <body> <h2>Alert Button</h2> <p> <External JavaScript file is myscript.js/p> <button type="button" onclick="sayHello()">Click</button> <script src="myscript.js"></script> </body> </html>
To use JavaScript from an external file source, you need to write all your JavaScript source code in a simple text file with the extension ".js" and then include that file as in the above structure i.e. <script src="myscript.js"></script>
Keep the following content in myscript.js file −
function sayHello() { alert("Hello World") }
- Related Articles
- How to include inline JavaScript inside an HTML page?
- How to create an inline frame using the tag inside an HTML page?
- How to use JavaScript to redirect an HTML page?
- How to include an acronym in HTML?
- How to include an abbreviation in HTML?
- How to set the page-break behavior inside an element with JavaScript?
- How to include an input field in HTML?
- How to include another HTML file in an HTML file?
- How to display JavaScript variables in an HTML page without document.write?
- How to add an element horizontally in HTML page using JavaScript?
- How to redirect from an HTML page?
- How to use an HTML tag inside HTML table?
- How do we include an anchor in HTML?
- How to Insert an Image in HTML Page?
- How to write JavaScript in an External File?
