• No se han encontrado resultados

Análisis de datos Anexo 2

Participante 6 Asistencia: 7 sesiones

JavaScript exists only within web pages. It is not compiled: its statements are interpreted when the page is loaded, and you cannot write stand alone JavaScript programs. Since it is not compiled, only a cursory syntax check is performed when the web page is loaded by the broswer, and errors can still occur when the program is executed which would be caught in compiled languages. Thus, you need to test JavaScript programs very carefully before making them available on your web pages.

JavaScript is primarily for computing strings and values: it does not itself have any GUI components, although you can refer to the basic Form controls within JavaScript. JavaScript has a close relationship to the browser and the web page. You can determine the history of pages the user has visited and change links accordingly and can change such things as background color dynamically.

While JavaScript has a C or Java-like syntax, using braces to set off blocks of code, it doesn’t really allow you to create objects or to derive new objects from existing ones. The main purpose of JavaScript is to allow you to perform some computations affecting the appearance of your web page, and to validate input data before sending it off to a CGI server. Since JavaScript does allow you to check for the consistency of your input data, we will spend the rest of this brief chapter explaining what you can do with this useful little language.

Embedding JavaScript Programs

A JavaScript program is a part of an HTML document, just like a frame or a table. It starts with the <script> tag and ends with the </script> tag.

<html> <body> <Script>

//JavaScript statements go here </script>

</body> </html>

You can also optionally specify the language which you are embedding. This will become more important as other web languages appear: <script language=”Javascript”>

JavaScript programs can appear anywhere on a web page, but if you write functions which are called from within those programs, the functions must appear in the <head> section of the web page. Thus guarantees that the functions will have been loaded by the time the JavaScript main program is detected and interpreted later on the web page.

<html> <head> <script> function square(x) { return x*x; } </script> </head>

You can also embed a JavaScript program from a separate file using the same “src=” tag we used to load images:

<script src=”myscript.js”>

For this feature to work properly, your web server must map the “.js” extension to the type “application/x-javascript” and send this type information back in the HTML header.

JavaScript Variables

Variables in JavaScript are not typed and do not need to be declared in advance. You can just use them as you need them:

x = 5; y = x/3; z = “Fred”;

However, Illegal operations such as a = z/2; //where z=”Fred”

are not discovered until you execute the program. All of the operators that you can use in Java, such as “++”, “+=”, and “>>” are also legal in JavaScript.

Variables are created where you first declare them and are all global: they hold the same values even between separate functions. You can create a local variable, whose scope is limited to the current function by preceding it with the var declaration:

var x = 5;

Let’s consider the following simple JavaScript program: <html>

<body> <Script>

for (i=0; i<10; i++)

document.writeln(i + " " + i/3 + "<br>"); </script>

</body> </html>

This simple web document is shown in Figure 24-1 and is on your example disk as math1.htm.

Figure 24-1: The numbers displayed by the math1.htm

JavaScript program.

Note that the for loop has exactly the same syntax as it would in Java. It is at first tempting to equate the Java statement

System.out.println(i + “ ” + i/3);

with the JavaScript statement

document.writeln(i + " " + i/3 + "<br>");

However, the Java statement writes characters to the standard output channel, usually a DOS or terminal window, while the JavaScript statement writes HTML code to a web page. Thus, you are not just writing text to a screen, but are writing HTML code that will be displayed according to the rules of web pages. For example, if we just wrote

document.writeln(i + " " + i/3);

without including the <br> tag, all of the numbers would be run together on a single line. Another way to force separate lines on the screen is to enclose

the text within <pre> (presentation) tags, as is done in the example code math2.htm.

<Script>

document.writeln(“<pre>”); for (i=0; i<10; i++)

document.writeln(i + " " + i/3 ); document.writeln(“</pre>”);

</script>

This also forces the output to be in a Courier or typewriter-style font as shown in Figure 24-2.

Figure 24-2: The numbers displayed by the math2.htm

JavaScript program.

So to summarize, writing output to a web page is actually writing HTML markup to a web page and you can affect the font size, layout, color, and style just as you would if you created a static web page. In fact, it is this construction of dynamic web pages that is one of JavaScript’s purposes.