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 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.