How to Use JavaScript Alerts Functions Variables

Q. What is JavaScript?

A. JavaScript was created in 1995 by Netscape Communications Corp by a man named Brendan Eich and is designed to bring interactivity to webpages and creating web applications. Originally called LiveScript, it is an object oriented language, meaning it can use and support objects. While it may be known to be similar to Java, its not. JavaScript contains smaller and simpler functions than Java. The one big difference is that Java is able to stand on its own, meaning that it doesn’t have to be embedded into an html page. While JavaScript has to be embedded into an HTML page.

It is by far the most popular language on the internet with letting developers become interactive with their viewers.

How to Use JavaScript embed

Q. How to use JavaScript.

A. JavaScript is very easy to use. First step would be to embed your JavaScript document. Below is how JavaScript is embedded into your HTML index page.

You open with a script tag. The  “src,” shortened for source, will let you browse for your JavaScript document.  After you locate your JavaScript document then you would close with a “” tag.

[code]<script src="script.js"></script>[/code]

After embedding your document into the HTML, on your JavaScript document you would start with “onload=init;”…this means the the document is telling the computer that when your page loads it will run the function called “init”.  After that is in your document, you would start with an opening function which tells the computer that when something is clicked or hovered over, it will perform a named function. Below is an example of how to start out your JavaScript document:


function init(){}

Q. How do I create a JavaScript Alert?

A. JavaScript alerts are one of the simplest things you can write. They are used for numerous reasons. For Example, when you visit a webpage and something pops up telling you that you are the “100th visitor and you’ve won $1000” that is a JavaScript Alert.

This is an example of a JavaScript Alert.


alert("Hello! I am an alert box!!");


Now lets associate it with the script document. When you add in a JavaScript Alert, it can go basically anywhere. They are not meant to be functions but rather to bring attention to what is in them. They don’t have to be in a certain function, but as long as they are in your document somewhere. Here is an example:



function init(){
alert("Hello! I am an alert box!!");


In this case, I have put my alert in the “init”.  So it would tell the computer that when the page loads (“onload”), it will run the function init (” function init(){}; “) which, in return, will run the alert.

Q. How do I create a JavaScript function?

A. JavaScript functions are they way of the computer communicating with it. They are very simple to write.

Each function contains 4 parts:

  • document

-This tells the computer that you are talking about the                                      document it is on.

  • getElementsByTagName(“nameoftag”)

-This can be many different things. The popular one is getElementByTagName(“a”)…with “a” being the tag name. But it can also be getElementById(“red”)...with “red” being the name of your id, which you would give a tag an id in your HTML document like this:


<body id="red">[/code]

-As you can see, I’ve given the body an id of “red”.

  • -.onclick=

-These are called event handlers, they tell the computer when to perform the function it is associated with. Like the previous example, these can also be different things. Like:

onload– tells the computer when the object is loaded to run

onmouseover-tells the computer when the object is hovered

onmouseout– tells the computer when the mouse is leaving                       what object it is hovered over

onmousedown-tells the computer when the user clicks on the object and doesn’t release the click.

onmouseup-tells the computer when the user releases the click.

onclick-tells the computer when the user clicks on an object

onchange-occurs when the value of an object is changed

  • function(){nameOfFunction();};

In between the curly braces is where your function would go that would tell the computer that when something is clicked or hovered over, it will perform the function. This is where each time you write a function, you will call upon that named function and tell the computer that you want that specific function performed when it is clicked.  In this case, when something is clicked, it will perform the function.


function init(){document.getElementsByTagName("a").onclick=function(){performThisFunction();};


Q. How to make JavaScript Variables.

A. Variables can be thought of as containers. They create and store a certain value that you put into it. When you make a variable, the first thing you want to do is name it. You can name your variable with a letter or you can name it. For Example:


var name= "your first name";[/code]

In the example above,  the variables name is “name”…with the variable storing “your first name”.

Variables in JavaScript must start with a letter. They can also begin with signs, such as $ or &. One thing to remember about JavaScript is that it is case sensitive. JavaScript is much like algebra. In algebra you use letters, such as x, to hold values, such as 6. If you were to add the two variables together, it would look like this z=x+y…with the answer being z=11.

[code]var x=5;
var y=6;[/code]

How to Use JavaScript Alerts Functions Variables was last modified: January 24th, 2014 by Maximus Mccullough

How to Use JavaScript

Leave a Reply

Your email address will not be published.