Saturday 4 April 2015

Introduction to JavaScript

JavaScript is denoted as the language for the web, for HTML, for servers, for PC, laptops, tablets and more. It is one of the most important language in the world. Today, most of the modern HTML pages uses JavaScript. JavaScript is used to denote the behaviour of the webpage.

JavaScript was invented by Brendan Eich. ECMA-262 is the official standard of JavaScript. JavaScript is a scripting language, that is a lightweight programming language that can be inserted into an HTML page and can be executed by all type of web browsers. One of the most important thing that we should remember is that Java and JavaScript are both different.

In HTML, JavaScript must be inserted between <script> and </script> tags. It is possible to put unlimited scripts in an HTML document. They can be put in both head section and body section. It is the common practise that the scripts be put in the head section or at the bottom of the page separating HTML code and JavaScript code, by putting the code at one place.

The <script>

To insert a JavaScript code in our HTML page, we use the <script> tag. The JavaScript code can be inserted between these tags.

For example:
<script>
function myFunction()
{
document.getElementById("demo").innerHTML="My first 
 JavaScript Function";
}
</script>

JavaScript in <head>

For example:
<!DOCTYPE html>
<html>

<head>
<script>
function.myFunction()
{
document.getElementById("demo_1").innerHTML="My first 
 JavaScript Function";
}
</script>
</head>

<body>
<h1>A Web Page</h1>
<p id="demo_1">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
</body>

</html>

JavaScript in <body>

For example:
<!DOCTYPE html>
<html>

<body>
<h1>A Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction1()">Try it</button>

<script>
function myFunction1()
{
document.getElementById("demo").innerHTML="My first 
JavaScript Function";
}
</script>
</body>

</html>

External JavaScripts

JavaScript can also be placed as external files. External files contain code to be used by many web pages. External JavaScript files are created with extension .js.

For example:
<!DOCTYPE html>
<html>
<body>
<script src="script.js"></script>
</body>
</html>

0 comments:

Post a Comment