Yammi
Yammi

Reputation: 735

Loading JS script for only iOS devices?

I have a jQuery script I'm using on a site to allow fixed position background images on iPhone/iPad/iPod. However it seems to be clashing with another script I am using on the site that enlarges background images full screen. Luckily they're independent of each other, I don't need the clashing background image script to work on iOS devices and vice versa.

Is there a way I can specifically target IOS devices to serve a JS file? I initially thought about using some kind of IF statement and doing it on window size but that started to get a bit complicated and affects other non-IOS devices. It just needs to run something like this...

..."if IOS device then load scroll.js"

I know device/browser sniffing is frowned upon but I can't think of another way around this problem.

Upvotes: 10

Views: 20956

Answers (3)

Justin Ethier
Justin Ethier

Reputation: 134167

You can use the Mobile Safari user agent string to detect mobile safari server-side, see: How do I detect Mobile Safari server side using PHP?


You can also do this in JavaScript:

if(navigator.userAgent.match(/(iPhone|iPod|iPad)/i))

See iPhone & iPod Detection Using JavaScript for more information.

Upvotes: 28

Kshitiz Ghimire
Kshitiz Ghimire

Reputation: 1716

you can also try this

if (navigator.userAgent.match(/like Mac OS X/i)) {
                alert('Hi, you\'re browsing from an iOS device.');
}

Upvotes: 1

Keltex
Keltex

Reputation: 26426

You can use Detect Mobile Browser (it has a library for javascript).

Upvotes: 1

Related Questions