if-then Statements

Now that you know which browser your visitor is using, you need to learn how to tell JavaScript to write different things depending on the browser being used—in other words, how to implement a logical test, choosing between different actions based on specific information. Branching is a fundamental technique in any programming or scripting language. Be sure to read this section if you're not already familiar with the concept.

To alter your web pages based on the browser a visitor is using, you tell JavaScript something like, "If the visitor is using Internet Explorer, then write this IE-tailored content."

An if-then statement in JavaScript looks like this:

if (navigator.appName == "Microsoft Internet Explorer") { // write IE-specific ...

Get The Book of JavaScript, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.