Monday 7 January 2013

introduction of JavaScript

 

JavaScript is a Scripting Language:

A scripting language is a lightweight programming language.
JavaScript is programming code that can be inserted into HTML pages.
JavaScript inserted into HTML pages, can be executed by all modern web browsers.

The <script> Tag

To insert a JavaScript into an HTML page, use the <script> tag.
The <script> and </script> tells where the JavaScript starts and ends.
The lines between the <script> and </script> contain the JavaScript:
 syntax:
<script>
alert("My First JavaScript");
</script>


JavaScript in <body>

Example:

<!DOCTYPE html>
<html>
<body>
.
.
<script>
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph</p>");
</script>
.

.
</body>
</html>

A JavaScript Function in <head>

Example:

<!DOCTYPE html>
<html>

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

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

 

A JavaScript Function in <body>

Example:


<!DOCTYPE html>
<html>
<body>

<h1>My Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
document.getElementById("demo").innerHTML="My First JavaScript Function";
}
</script>

</body>
</html>

External JavaScripts

Scripts can also be placed in external files. External files often contain code to be used by several different web pages.
External JavaScript files have the file extension .js.
To use an external script, point to the .js file in the "src" attribute of the <script> tag:

Example:

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

Manipulating HTML Elements

To access an HTML element from JavaScript, you can use the document.getElementById(id) method.
Use the "id" attribute to identify the HTML element:

Example 

Access the HTML element with the specified id, and change its content:

<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>

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

</body>

</html> 

Writing to The Document Output:

The example below writes a <p> element directly into the HTML document output:

Example

<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>

<script>
document.write("<p>My First JavaScript</p>");
</script>


</body>
</html>

 

 
download javascript tutorial in pdf format:
http://www.tutorialspoint.com/javascript/javascript_tutorial.pdf
www.ohio.edu/technology/.../java-script-reference-guide.pdf
  
learn javascript websites:
www.w3schools.com/js/default.asp
www.js-examples.com/

No comments:

Post a Comment