{"id":789,"date":"2012-04-08T16:13:26","date_gmt":"2012-04-08T23:13:26","guid":{"rendered":"http:\/\/zsprawl.com\/iOS\/?p=789"},"modified":"2012-04-08T22:13:32","modified_gmt":"2012-04-09T05:13:32","slug":"detecting-device-and-orientation-in-javascript","status":"publish","type":"post","link":"http:\/\/zsprawl.com\/iOS\/2012\/04\/detecting-device-and-orientation-in-javascript\/","title":{"rendered":"Detecting Device and Orientation in Javascript"},"content":{"rendered":"<p>Here is the code that I&#8217;m using in Javascript to determine whether or not I have an iPhone or an iPad in Portrait or Landscape mode.<\/p>\n<p>[code]function isiPhone() {<br \/>\n    if (( navigator.platform == &#8216;iPhone&#8217; ) ||<br \/>\n           ( navigator.platform == &#8216;iPod&#8217; ) ||<br \/>\n           ( navigator.platform == &#8216;iPhone Simulator&#8217;)) {<br \/>\n        return true;<br \/>\n    } else {<br \/>\n        return false;<br \/>\n    }<br \/>\n    return false;<br \/>\n}<\/p>\n<p>function isiPad() {<br \/>\n    if (( navigator.platform == &#8216;iPad&#8217; ) ||<br \/>\n          ( navigator.platform == &#8216;iPad Simulator&#8217; )) {<br \/>\n        return true;<br \/>\n    } else {<br \/>\n        return false;<br \/>\n    }<br \/>\n    return false;<br \/>\n}<\/p>\n<p>function isLandscape() {<br \/>\n    if (isiPhone()) {<br \/>\n        if ($(window).width() >= 480)   {<br \/>\n            return true;<br \/>\n        } else {<br \/>\n            return false;<br \/>\n        }<br \/>\n    }<br \/>\n    if ($(window).width() >= 1024)   {<br \/>\n        return true;<br \/>\n    } else {<br \/>\n        return false;<br \/>\n    }<br \/>\n    return false;<br \/>\n}[\/code]<\/p>\n<p>The code is pretty straight-forward. I put it in a separate .js file, and then include it with most of my projects.<\/p>\n<p>I added a new one yesterday to detect the version of iOS because the <a href=\"http:\/\/zsprawl.com\/iOS\/2012\/04\/instagram-like-core-image-filters-in-ios\/\">ImageFilter plugins<\/a> that I&#8217;m using in <a href=\"http:\/\/zsprawl.com\/iOS\/zapps\/demotivate-hd\/\">Demotivate HD<\/a> is only supported in 5.0 and later. I didn&#8217;t want to piss off my users by increasing the minimum requirement, so this worked nicely.<\/p>\n<p>[code]function isOld() {<br \/>\n    if (\/OS [1-4]_\/i.test(navigator.userAgent)) {<br \/>\n        return true;<br \/>\n    } else {<br \/>\n        return false;<br \/>\n    }<br \/>\n    return false;<br \/>\n}[\/code]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is the code that I&#8217;m using in Javascript to determine whether or not I have an iPhone or an iPad in Portrait or Landscape mode. The code is pretty straight-forward. I put it in a separate .js file, and then include it with most of my projects. I added a new one yesterday to <a href=\"http:\/\/zsprawl.com\/iOS\/2012\/04\/detecting-device-and-orientation-in-javascript\/#more-'\" class=\"more-link\"><br \/>more \u00bb<\/a><\/p>\n","protected":false},"author":318,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,3,25],"tags":[9,7,6,120],"class_list":["post-789","post","type-post","status-publish","format-standard","hentry","category-blog","category-ios-development","category-jquery-mobile","tag-development","tag-ipad","tag-iphone","tag-jquery-mobile"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/zsprawl.com\/iOS\/wp-json\/wp\/v2\/posts\/789"}],"collection":[{"href":"http:\/\/zsprawl.com\/iOS\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zsprawl.com\/iOS\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zsprawl.com\/iOS\/wp-json\/wp\/v2\/users\/318"}],"replies":[{"embeddable":true,"href":"http:\/\/zsprawl.com\/iOS\/wp-json\/wp\/v2\/comments?post=789"}],"version-history":[{"count":8,"href":"http:\/\/zsprawl.com\/iOS\/wp-json\/wp\/v2\/posts\/789\/revisions"}],"predecessor-version":[{"id":806,"href":"http:\/\/zsprawl.com\/iOS\/wp-json\/wp\/v2\/posts\/789\/revisions\/806"}],"wp:attachment":[{"href":"http:\/\/zsprawl.com\/iOS\/wp-json\/wp\/v2\/media?parent=789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zsprawl.com\/iOS\/wp-json\/wp\/v2\/categories?post=789"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zsprawl.com\/iOS\/wp-json\/wp\/v2\/tags?post=789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}