HTML and CSS Reference
In-Depth Information
When the Web page opens, you see the contents of the document.write() statement. h e
write() method is just a built-in function that expects a line of text to display on the screen.
In this case, the text is from an external i le; otherwise, it's the same as embedding it in a Web
page script.
JavaScript functions are packages of code that are launched when called by the Web page. h e
advantage of functions is that you can use them to package code and make changes to add
new content. h e built-in write() function only requires that you enter some text for it
print to the document (Web page). You don't have to rely on built-in functions but can create
your own. For example, the following is an external JavaScript program with a simple
function that opens an alert() function. (A user function using a built-in function.) Save
the following JavaScript program as nameMe.js :
// JavaScript Document
var name = ”Little Willie Hacker” ;
function getName ( someName )
alert ( someName );
getName ( name )
All functions are followed by parentheses. If required, the developer can put a parameter in
the parentheses. In this case, the parameter is called someName . When the function is
called, the developer places a name, a number, or anything else desired in the space where
someName is. In this case, a variable labeled name is assigned the value Little Willie
Hacker . At the bottom of the program, the line, getName(name) calls the function, placing
the variable into the parameter. h e function passes the value of the variable to the alert()
function within the getName() function, so you can expect to see an alert box on the screen
when the program launches. h e following HTML5 ( JSfunction.html in this chapter's
folder at ) calls the JavaScript that calls the
< html >
< head >
< script src = ”nameMe.js” ></ script >
< meta http - equiv = ”Content-Type” content = ”text/html; charset=UTF-8” >
< title > External Function </ title >
</ head >
< body >
</ body >
</ html >
h at JavaScript program launched as soon as the page loads. A more important use of
JavaScript functions lies in its ability to wait for the JavaScript until it needs it. h e next
section shows how.
Search WWH ::

Custom Search