Only Add a Script on iOS Devices

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.

Share this:

Email
Facebook
Twitter
Pinterest
Pocket

Premium Themes for WordPress

Looking for an easy-to-use Premium Theme for WordPress? Check out Themes by bavotasan.com and have your site up and running in no time.

Use this WordPress website builder to build powerful websites in no time for your or your clients.

WordPress Hosting

WP Engine – designed from the ground-up to support and enhance the workflow of web designers.

Bluehost – providing quality web hosting solutions since 1996.

About the author

Picture of Luke Perrie

Luke Perrie