pTutorial

New way to learn Programming

How to include external javascript in html

External JavaScript

In this document we have discussed about how to include external JavaScript file in the html page. This tutorial contains the external JavaScript tutorial with the help of example. This JavaScript example is also available for download.

Need of external JavaScript file?

If you want to use the same JavaScript function or block of code in different pages of a website, you should create JavaScript function (block of code) and save this file with .js extension and after that use this .js file with the help of src attribute.

It increases the code readability.

Code reusability.

External JavaScript Example

// JavaScript Document
//first.js
function firstExample() 
{
   alert("External js example")
}

Note There is no need to write code inside the <script> tag.

Html file

<html>

<head>
<script src="http://www.ptutorial.com/first.js" 
type="text/javascript"  ></script>
</head>

<body>
<h2>External JavaScript Example</h2>
<input type="button" onClick="firstExample()" value="check js ">

</body>
</html>

Download

See Also


QUOTES OF THE DAY

The basis of all progress is self-reliance

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER