Detecting mobile devices with Javascript

Today at work we were struggling with a way to detect the iPad and similar devices without relying on the browser user agent string.

We ended up checking the value of window.onorientationchange like so:

function is_mobile_device() {
    if (typeof window.onorientationchange != "undefined") {
        return true;
    }
    else {
        return false;
    }
}

This function will return false in all current desktop browsers because typeof window.onorientationchange is “undefined.” On the iPad and other mobile devices it will return a type of “object.”

What other ways are you using to detect mobile devices?

One thought on “Detecting mobile devices with Javascript

  1. this is the simplest detection code I could find, thanks.

    I simplified it a little more though…

    var is_mobile = (typeof window.onorientationchange != “undefined”);

Comments are closed.