Home Snippets Projects Donate

Get Snippets For:

   Java    HTML/CSS JavaScript

External JavaScript Association

Code Description:

To use an external JavaScript file from your HTML code, you can add a <script> tag with the "type" attribute set to "text/javascript" and the source (src) set to the path and file name. This needs to be declared in the Head of your HTML file. In our example, we have a simple button that calls an alert() function from an external JavaScript file.

JavaScript External Link Syntax:

<script type="text/javascript" src="pathAndFileName.js"></script>

Example JavaScript File (Named "javascript.js"):

function alertMe() {
   alert('The button of POWER has been clicked.');

Runnable External JS Example Code:

<!DOCTYPE html>
        <title>External JS Example</title>
	<script type="text/javascript" src="javascript.js"></script>
        <button onclick="alertMe()">Click Me!</button>

How to Run This Code:

Simply paste this code into a plain text file with the ".html" extension and save it. The JavaScript code should be placed into a text file name "javascript.js". You will then be able to open it in the web browser of your choice.


JavaScript Externally Associated Output Image

Related Links: