Lesson 6: Built in JavaScript Functions Alert():


In lessons 6,7 and 8 we will cover some of the most usefull built in fuctions. We will try to list all of them by the end of this guide. We will start by explaining some of the most commonly used built in functions like the fuctions that are used to produce message alerts and other pop up windows.


Before we continue, I want to take a brief moment to explain the difference between functions and methods in JavaScript. Okay, that was a trick statement, because often there is no difference between them. Often, things can be both. The next few functions are prime examples. Alert, prompt and confirm boxes are very often referred to as methods rather than functions. The truth is that they are both. JavaScript has no clear distinction between functions and methods. This is often very confusing to beginners. I know it drove me nuts when I was learning JavaScript for the first time. You will have to learn not to be too strict in calling things functions, methods or even objects when using JavaScript as the terms are very often interchangable. With that said, let's continue with the alert method or the alert function.


Alert Function:


The alert function displays information in a dialog box. You can use this to display error messages. It is particularly usefull when used with form validation (you will learn that later). we will be using the alert function often in this guide because it is an easy way to demonstrat the results of our code. You may remember that we used an alert box for our "Hello World!" example in lesson 4. Here is an example of the alert function:


Structure:



alert("Put what you want in the alert box here")



Working Example:


Copy this code into the body section of your template right after the opening script tag:


var alertMessage = "This is an alert that's in a variable.";

alert(alertMessage)


Put that code inside the script tags and a messagebox will pop up on the screen that says: "This is an alert that's in a variable." as soon as the page is opened. That is the bare bones style example to show you the minimal requirements to make the function work.


Now some of you might be saying that's not how you showed us a function works! Well it is a bit different yes, but really the concept is the same, alot of the code is built in so you can not see the code for the actuall making of the function, that has allready been done for you. All you have to do is call it! That goes for all the built in functions we will be studying. Just so you can get a better idea of what the alert function is commonly used for, I am going to demonstate a very simple form validation script:


First we need a form to validate:



<html>

<head>

<title>Code Template</title>

</head>

<body>

<form method="post" action="JS.js">

Name:<br>

<input type="text" id="name" name="name"> <br>

<input type="button" value="submit" onClick="check()">

</form>

</body>

</html>



Lesson 6 is continued on the next page. Just click the arrow below to continue:

                    

Copyright © 2012 VisualBuilder. All rights reserved