Detect a device is iOS or not using JavaScript
In order to detect a device whether it is iOS or not. We’re going to Navigator platform and Navigator userAgent property.
-
Navigator userAgent property
This property returns the value of the user-agent header which is sent by the browser to the server.
Returned value, have information about the name, version, and platform of browser.
Syntax:navigator.userAgent
Return value:
It returns a string, denoting the user agent string for the current working browser. -
Navigator platform property
This property returns the platform for which the browser is compiled.
Syntax:
navigator.platform
Return value:
It returns a string, representing platform of browser.
Possible values.- HP-UX
- Linux i686
- Linux armv7l
- Mac68K
- MacPPC
- SunOS
- Win32
- Etc.
Example 1:This example detects the device by (navigator.userAgent) property and returns false.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Detecting a device is iOS. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> Detecting whether a device is iOS. </p> <button onclick="gfg_Run()"> detect </button> <p id="GFG_DOWN" style="color:green; font-size: 23px; font-weight: bold;"> </p> <script> var el_down = document.getElementById("GFG_DOWN"); function gfg_Run() { var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream; el_down.innerHTML = iOS; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: This example detects the device by (navigator.platform) property and returns true.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Detecting a device is iOS. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> Detecting whether a device is iOS. </p> <button onclick="gfg_Run()"> detect </button> <p id="GFG_DOWN" style="color:green; font-size: 23px; font-weight: bold;"> </p> <script> var el_down = document.getElementById("GFG_DOWN"); function gfg_Run() { var iOS = !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform); el_down.innerHTML = iOS; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 3:This example detects the device by (navigator.platform) property and returns false.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Detecting a device is iOS. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> Detecting whether a device is iOS. </p> <button onclick="gfg_Run()"> detect </button> <p id="GFG_DOWN" style="color:green; font-size: 23px; font-weight: bold;"> </p> <script> var el_down = document.getElementById("GFG_DOWN"); function gfg_Run() { var iOS = !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform); el_down.innerHTML = iOS; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to detect the device is an Android device using JavaScript ?
- How to detect touch screen device using JavaScript?
- How to detect a mobile device in jQuery?
- How to detect the user's device using jQuery ?
- How to get the height of device screen in JavaScript ?
- How to get the width of device screen in JavaScript ?
- How to detect browser or tab closing in JavaScript ?
- How to detect flash is installed or not using JavaScript ?
- Detect the Operating System of User using JavaScript
- How to detect HTML 5 <canvas> is not supported by JavaScript ?
- How to detect operating system on the client machine using JavaScript ?
- How to detect the change in DIV's dimension ?
- How to detect if a function is called as constructor?
- How to detect search engine bots with PHP ?
- How to detect HTML 5 is supported or not in the browser ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



