Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

Javascript - Tutorial

Lars Vogel

Version 1.1

27.04.2011

Revision History
Revision 0.131.05.2009Lars Vogel
created
Revision 0.2 - 1.1 13.06.2009 - 27.04.2011Lars Vogel
bug fixes and enhancements

Learning JavaScript

The article contains a introduction to JavaScript and JSON.


Table of Contents

1. JavaScript Overview
1.1. What is JavaScript?
1.2. Your first JavaScript (embedded))
1.3. Your first JavaScript (external))
2. Debugging
3. Programming in JavaScript
3.1. Variables
3.2. Arrays
3.3. Functions
3.4. Prototype in JavaScripts
3.5. Assigning functions to HTML buttons
3.6. HTML Events
4. JavaScript and HTML interaction
4.1. HTML DOM
4.2. Manipulation HTML with JavaScript
5. JSON
6. Examples
6.1. Create a dynamic link using the webpage information
6.2. Reading META Tags via JavaScript
6.3. HTML forms and JavaScript
7. Thank you
8. Questions and Discussion
9. Links and Literature

1. JavaScript Overview

1.1. What is JavaScript?

JavaScript is a scripting language which is primary used within HTML webpages. JavaScript is executed directly in the browser and all main browsers contain a compiler or interpreter for JavaScript. JavaScript is case sensitive. JavaScript and Java are completely different programming languages even though they have a similar name.

You can put JavaScript into an external file or directly into the HTML page. If you put the JavaScript code into the HTML page you can either put it into the header or in the body of the HTML page. JavaScript which is embedded into an HTML page must be surrounded by <script type="text/javascript"> </script> tags. The type is mandatory, even if the browser only supports JavaScript.

JavaScripts in the body will be executed while the page loads. JavaScripts in the header will be executed when other JavaScript functions in the body call them.

1.2. Your first JavaScript (embedded))

Create the following HTML page (via a standard text editor) and open it with a browser. In this example we will embedded the JavaScript into the HTML page.

				
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
<!--This is single line comment --> 
/*
This is a multi line commend
The following command write output the text into the HTML code. -->
*/
document.writeln("Hello, JavaScript!");
</script>
</body>
</html>
			

Open the HTML page in a browser. The commands within the script tags will run and "Hello, JavaScript!" will be written to the webpage.

1.3. Your first JavaScript (external))

You can also put the JavaScript in a separate file and include it into the HTML page. For example create the file "helloworld2.html" with the following content.

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

Create the file "myfirstjsfile.js" in the same directory as the HTML page. As the JavaScript is in a separate file you don't have to to use the <script> </script> tag. Simple write the code directly into the file.

				
document.writeln("<b>Bold Hello World via an external js file!</b>");
			

Open the HTML page in a browser. The script should get executed and you should see the message.