userAgent string and take appropriate action.
if ((navigator.userAgent).indexOf("Mobile")) alert("Mobile Device");
/expression/i makes the search case insensitive.
if (/Android|webOS|iPhone|iPad|BlackBerry|Windows Phone|Opera Mini|IEMobile|Mobile/i.test(navigator.userAgent)) alert("Mobile Device");
This code covers many of the popular mobile devices on market, but not all of the minor players. Also the userAgent strings are constantly subject to change as new versions come up and we can only hope the userAgent strings still have these words.