You are here: JAVAScript Tutorial

JavaScript is the programming language for developing web application.
JavaScript is the most popular programming language for client side in the world.
JavaScript can be placed in the <body> or the <head> sections of an HTML page.
In HTML, JavaScript code  must be inserted in <script> tag.
Older way, you can use <script type="text/javascript">.
The type attribute is not required because JavaScript is the default scripting language in HTML.

Example: 

<script>
document.getElementById("demo").innerHTML = "My First JavaScript"; 
</script>

JavaScript in <head>:

In this example, a JavaScript function is in the <head> section of an HTML page.

<!DOCTYPE html>
<html>
<head>
<script>
 
function myFunction() {
document.getElementById("nicedemo").innerHTML = "Paragraph changed.";
}
</script>
</head>
<body>
<h1>My Web Page</h1>
<p id="nicedemo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>


JavaScript in <Body>:

In this example, a JavaScript function is in the <body> section of an HTML page.

<!DOCTYPE html>
<html>
<head>
</head>
<body>
 
<h1>My Web Page</h1>
 
<p id="nicedemo">A Paragraph</p>
 
<button type="button" onclick="myFunction()">Try it</button>
 
<script>
 
function myFunction() {
document.getElementById("nicedemo").innerHTML = "Paragraph changed.";
}
 
</script>
</body>
</html>

External JavaScript:


Scripts can also be placed in external files with .js extension.
External scripts are more practical when the same code is used in many different web pages.

To use an external script, put the name of the script file with path in the src (source) attribute of the <script> tag:

<!DOCTYPE html>
<html>
<body>
<script src="myScript.js"></script>
</body>
</html>

You can place an external script reference in <head> or <body> as you require.

 

Blog

Active User (0)

No Active User!
 Log In to Chat