Nowadays, every website is designed as a responsive so that it can be visited on any resolution window. But browser does not behave same on all the devices. For that, In CSS, we use media query. But using css, we can’t generate html fully compatible with all the devices.
I wanted to post another useful snippet of code for iPad/iPhone detection using javascript. If the person viewing the website is on iPad then we can place the condition according to iPad. It’s simple a javascript
For iPad detection:
1 2 3 4 5 6 7 8 9 | <script type="text/javascript"> var isiPad = navigator.userAgent.indexOf('iPad') != -1 //or var ua = navigator.userAgent; var isiPad = /iPad/i.test(ua) </script> |
For iPhone detection:
1 2 3 4 5 6 7 8 9 | <script type="text/javascript"> var isiPhone = navigator.userAgent.indexOf('iPhone') != -1 //or var ua = navigator.userAgent; var isiPhone = /iPhone/i.test(ua) </script> |
For iOS devices:
1 2 3 4 5 6 7 8 9 | <script type="text/javascript"> var isiOS = (navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1) //or var ua = navigator.userAgent; var isiOS = /iPhone/i.test(ua) || /iPod/i.test(ua) || /iPad/i.test(ua) </script> |
doesn’t work using WKWebView