Section E. Miscellaneous


E. Miscellaneous

We close our survey of the window object with a discussion of a few miscellaneous methods and properties.

navigator

Restrictions

In the past, focus() and blur() have been used for less-desirable actions, for instance opening a popup, blurring it, and then moving it beyond the edge of the computer screenin effect, creating a hidden popup. Therefore, more and more browsers allow their users to switch off the focus()/blur() and move/resize methods. This means that you cannot assume that all browsers that your visitors might use will allow these actions.


The navigator object contains properties that say something about the user's browser. By far the most important one is navigator.userAgent, which contains the entire userAgent string of the browser. The problem is not accessing this string, but interpreting it; in other words, detecting the user's browser. In 3D, we already discussed browser detection and why not to rely on it.

The navigator object contains other properties, including platform, appName, and appVersion, but they are less reliable than userAgent.

One other useful property, navigator.cookieEnabled, is covered in 6G.

alert, confirm, and prompt

The window object has three methods that cause dialog boxes to open: alert(), confirm(), and prompt(). The alert() method opens a dialog box with the argument as text. The confirm() method does the same, but gives the user an OK and a Cancel button. This method returns true (user clicked OK) or false (user clicked Cancel). The prompt() method allows the user to type in a string, and returns that string.

Example:

alert('Do you have a name?'); var hasName = confirm('Do you have a name?'); var name = prompt('Do you have a name?'); 


Figure 6.4. Alert, confirm, and prompt dialog boxes.


Now hasName contains true or false, while name contains the name the user typed in.

Note that the 'Do you have a name?' string shown in the dialog boxes is plain text, not HTML. Therefore, if you want a line break to occur in the box, you should use \n (newline character), not <br />.

alert('Do you have a<br />name?'); alert('Do you have a\nname?'); 


Figure 6.5. Dialog boxes contain plain text, not HTML.


In 3E, we discussed the use of alert() in JavaScript debugging.

confirm() and prompt() are rarely used, because, from a usability perspective, it's better to have users enter information in form fields and then read it out than to confront them with potentially confusing extra boxes.

Timeouts and intervals

Timeouts and intervals allow you to specify a bit of script that should be run after a certain time has elapsed. For instance:

setTimeout('myFunction()',1000); 


Now the function myFunction() is executed 1000 milliseconds after the browser receives the setTimeout command. Note the two arguments this method requires:

  1. A string, which is evaluated (interpreted as a JavaScript command) when the timeout runs out.

  2. The waiting time in milliseconds.

It's also possible to use a function reference instead of a string:

setTimeout(myFunction,1000); 


Now the same thing happens. The advantage of this method is that your code is cleaner (no complicated quotes), but the disadvantage is that you cannot send arguments to the function.

clearTimeout

It is also possible to cancel a timeout. To do this, you have to assign the timeout to a variable. After all, if you want to cancel it, JavaScript should be able to access the timeout.

var myTimeOut = setTimeout('myFunction()',1000); 


This line of code does exactly the same thing as the previous line. However, since we assigned the timeout to a variable, we can use the clearTimeout method to cancel the command:

clearTimeout(myTimeOut); 


If the timeout hasn't yet taken place, it's canceled, and myFunction() is not called.

Intervals

Intervals are timeouts that are run repeatedly. This line commands myFunction() to be executed in 1000 milliseconds, and to repeat it every 1000 milliseconds until further notice:

setInterval('myFunction()',1000); 


Just as with timeouts, you can cancel the interval:

var myInterval = setInterval('myFunction()',1000); clearInterval(myInterval); 


Use of timeouts and intervals

Timeouts and intervals are useful whenever you want to defer the execution of code, or if you want a specific bit of code to run repeatedly. We already saw an example of the first situation in the trackMain() function of Site Survey.

[Site Survey/popup.js, lines 34-37]

else if (tryCounter < nrOfAttempts) {    setTimeout('trackMain()',waitingTime);    tryCounter++; } 


If the status of the main window is uncertain, the function should run again after a certain number of milliseconds, as defined in waitingTime.

Timeouts and intervals are crucial for JavaScript animations. We'll study those in 9G.



ppk on JavaScript. Modern, Accessible, Unobtrusive JavaScript Explained by Means of Eight Real-World Example Scripts2006
ppk on JavaScript. Modern, Accessible, Unobtrusive JavaScript Explained by Means of Eight Real-World Example Scripts2006
ISBN: N/A
EAN: N/A
Year: 2005
Pages: 116

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net