iPhone, iPad and iPod Touch detection using JavaScript

A function that detects if the users are browsing from an iPhone, an iPad or an iPod Touch and also adds a matching class name (iphone, ipad or ipod) to the HTML tag for a further easy CSS styling.

by XTREME104

JavaScript

/*

   Copyright (c) 2010 Sebastien P.

   http://twitter.com/_sebastienp

   MIT licensed.

   ---

   A function that detects if the users are
   browsing from an iPhone, an iPad or an
   iPod Touch and also adds a matching class
   name (iphone, ipad or ipod) to the HTML
   tag for a further easy CSS styling.

*/

(function (HTML_TAG) {

    "use strict";

    var device_name = /ip(hone|(a|o)d)/i.exec(this.navigator.userAgent),
    old_class_name = HTML_TAG.className;

    if (device_name) {

        device_name = device_name[0].toLowerCase();
        HTML_TAG.className = (old_class_name) ? (old_class_name + " " + device_name) : device_name;

    }

}(this.document.documentElement));