HTML with JavaScript

Using JavaSCript you can add interactivity in your HTML document. JavaScript make the website more attractive, useful and dynamic.

Example:

<html>
<body>
<h1>JavaScript date example </h1>
<button type="button" onclick="document.getElementById('demo').innerHTML = Date()"> Click Me! </button>
<p id="demo"> </p>
</body>
</html>

Run Example

<script> tag

To write JavaScript code in HTML document you have to use <script> tag.

Example:

<html>
<head>
<script type="text/javascript">
function jsexample() {
alert("Hello this is js within head tag!")
}
</script>
</head>
<body>
<h1>JavaScript within head tag </h1>
<input type="button" onclick="jsexample()" value="Click Me!">
</body>
</html>

Run Example

Using External JavaScript

to use external javascript you have to create an javascript file with .js extension and link this file to the HTML document. Here is an example using external JavaScript file named myJs.js:

In myJs.js:

function jsexample() {
alert("Hello this is js in external file!")
}

In HTML document:

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

Run Example