Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 797
Yammi Avatar asked Feb 02 '11 14:02

Yammi


1 Answers

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.

like image 77
Justin Ethier Avatar answered Oct 01 '22 19:10

Justin Ethier