Controlling Your JavaScripts

Some scripts you write will be simple. They will execute the same way every time, one time per page. If you add a JavaScript to play a sound when users visit your home page, for example, it doesn't need to evaluate any conditions or do anything more than one time. More sophisticated scripts might require that you take different actions under different circumstances. You might also want to repeat the execution of a block of code—perhaps by a set number of times or as long as some condition is TRUE. JavaScript provides constructs for controlling the execution flow of your script based on conditions, as well as for repeating a sequence of operations.

Testing Conditions

JavaScript provides a single type of control statement ...

Get Platinum Edition Using XHTML™, XML, and Java™ 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.