O'Reilly logo

The Book of JavaScript, 2nd Edition by thau!

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required