External JavaScript File

The src attribute of the script tag is used to add or include external JavaScript files into any number of HTML files. It increases the code reusability.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<script type="text/javascript" src="external.js"></script>
<script type="text/javascript" src="external.js"></script>
<script type="text/javascript" src="external.js"></script>

First, create a JavaScript file and then save the file with the .js extension. After that, we can use the src attribute of the script tag to use this already-created file.

test.js

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function show() {
alert("External JavaScript file.");
}
function show() { alert("External JavaScript file."); }
function show() {
alert("External JavaScript file.");
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<html>
<body>
<form>
<input type="button" value="Show" onclick="show()"/>
</form>
<script src="test.js"></script>
</body>
</html>
<html> <body> <form> <input type="button" value="Show" onclick="show()"/> </form> <script src="test.js"></script> </body> </html>
<html>
<body>
<form>
<input type="button" value="Show" onclick="show()"/>
</form>
<script src="test.js"></script>
</body>
</html>

Related topics: