Only Add a Script on iOS Devices
by c.bavota | Posted in Tutorials | 5 comments
Apr
15
2012
I was working on a site that required a small jQuery function to load only if the site was opened on an iOS device, ie. the iPad, iPhone or iPod. All you need to do is include a little call to the window useragent to test if the browser being used is an iOS browser.
Here is the code snippet:
userAgent = window.navigator.userAgent;
if(/iP(hone|od|ad)/.test(userAgent)) {
// add your script here
}
If you wanted to add a script to regular browsers but not to iOS browsers you can just modify the above snippet like so:
userAgent = window.navigator.userAgent;
if(/iP(hone|od|ad)/.test(userAgent)==false) {
// add your script here
}
It’s nothing that fancy but it works.



very handy indeed, the only thing you haven’t taken into account is that there are also other mobile browsers than just Apple’s
This is a handy little script and comes just in time for me as I try to integrate a flip book into a site design… one for devices that support Flash and the iPad. Yes, I know. I’m still looking for some solutions to bypass Flash all together.
Wouldn’t it be better to think about your browser’s capabilities than its id? For example, when using Modernizr ( http://modernizr.com/docs/ )
if (Modernizr.touch){ // do stuff }True. This was just a code snippet that didn’t rely on any other script. The Modernizr approach is great if you decided to go that route.
As Far as I know you have to check the device via JS and return double-sized images in order to take advantage of the new retina display.