Lesson 4: your first script



In this lesson we will get down and dirty and experiment a little with our first working script! We will introduce some actual working code to demonstrate one thing that you will be able to do with javascript. We will be intoducing a couple of built in functions and demonstate the use of variables and structure. Okay, lets look at some code:


var x = "hello ";

var y = "world!";

var z = x + y;

document.write(z);


Code explained:


On the first line we have declared variable x and assigned the string: "hello " to it. On the second line we declared variable y and assigned "world" to it. In the third line we introduce var z and assigned it to the value: x + y. Since x and y are string variables, they will be concatenated. In the last line we introduce the built in write() function and pass the var z to it. When the code is ran it will produce a page that says: hello world!


You Try It:



  1. Open your JStemplate.txt file.

  2. Put the above four lines of code in the head section of your template.

  3. Save it under its new name, alertz.html with the .html extension and close the file.

  4. Go to the folder you saved alertz.html to and open it.


If it opened a blank html page with an alert box saying "hello world!" then it worked! Congratulations you have completed your first successfull script!

                    

Copyright © 2012 VisualBuilder. All rights reserved