Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect IOS 8 with jQuery

Tags:

jquery

ios

I have googled and and tried many ideas but they either mess up the entire site on all browsers, or they are really old, or they are javascript and not jQuery. I am exhausted.

Currently this works for detection IOS, but not a version:

$(document).ready(function() {
    var deviceAgent = navigator.userAgent.toLowerCase();
    var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
    if (agentID) {

... do something here

    }
});    

How can I add just IOS 8 detection inside this so I can just create an html class "ios-8" "no-ios8" or equivalent.

like image 696
Christina Avatar asked Oct 14 '14 14:10

Christina


1 Answers

Mobile Safari's UA string looks like this:

Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12A405 Safari/600.1.4

(Source: http://whatsmyuseragent.com/)

So you could write a function like this. (DEMO)

var isIOS8 = function() {
  var deviceAgent = navigator.userAgent.toLowerCase();
  return /(iphone|ipod|ipad).* os 8_/.test(deviceAgent);
}

alert("isIOS8: " + isIOS8());

Visit the demo page in a mobile safari browser, or webview app like iOS Chrome, to see it alert true.

like image 164
Palpatim Avatar answered Oct 19 '22 18:10

Palpatim