10

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.

1
  • Use the user-agent matching in @Keltex's link. Feb 2, 2011 at 15:02

3 Answers 3

28

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.

1

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

1
  • 1
    Seems like a lot of code to just detect specifically IOS, is there any reason why that would be a more suitable solution over the answer Justin Ethier gave? Thanks for your help.
    – Yammi
    Feb 2, 2011 at 18:45
1

you can also try this

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.