Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Basic JavascriptTutorial

Welcome to my firstjavascript tutorial. I want to explain the basic structure of an document. Later, I will show you how to use variables, build basic functions with parameters. There are two types of using javascript in our html / php file. The first one is defining a <script> section, like this:
<script type= text/javascript > /* Javascript (multi line comment) */ //One line comment </script>

Instead of the parameter type= text/javascript you can also use language= javascript . The second possibility is creating an external javascript file with the file extension *.js

Go ahead and create a new textdocument. Change the filename to something like script.js. In your html file you can simple write:
<script type= text/javascript src= script.js ></script>

Now, every code which is used in the file script.js will be executed.

A simple test expression, for make sure, that it works is:


alert( test ); //This will popup a messagebox with the text given in the first and only parameter.

In your *.js file you must not use the <script> tags.Only write down the simple expression.

In Javascript like in the most programming languages there are also Variables. In Javascript you define a variable like this:
Varvariable; variable = test //This is called a string. Thereareintegers, floatsandmore.

or:
Var variable = test ; //String Var variable2 = 1337; //Integer

For example:
varmsg = Hello World ; alert(msg);

A messagebox with the text Hello World should appear now, when executing the script.

In your html file place a link like this, to call a javascript function:
<a href= javascript: [function] >Click</a>

The function must have been defined above. If [function] is givemeanalert() then the function must have been defined like this:
<script type= text/javascript > Functiongivemeanalert() { alert( test ); } </script>

If you click on the link, a messagebox with the content test should appear.

If you want to use parameters in functions:


<script type= text/javascript > Functionbananas(count) { alert( Youhavegot + count + bananas! ); } </script> <a href= javascript: bananas(5) >How much bananas do I have ?</a>

That s all. Note: This tutorial is made for a friend. He s got much experience in using other languages. If you can t understand, please use another tutorial for learning javascript. Sorry.

You might also like