How to detect whether a device is iOS without using Regular Expression in JavaScript

We can detect whether the device is iOS or not without using RegExp with the help of JavaScript. There are two approaches first is Use navigator.platform property to check for the particular keywords which belongs to iOS devices using index of( ) and second is using pop() to take out one by one

Approach 1: Use navigator.platform property to check for the particular keywords which belongs to iOS devices using indexOf() method.

<!DOCTYPE html>
<html>
    <head>
        <title>
            Detect whether a device is iOS 
            without using RegExp
        </title>
    </head>
    <body style="text-align: center;">
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
        <p>
            Click on the button to detect 
            whether a device is iOS or not?
        </p>
        <button onclick="gfg_Run()">
            Click Here
        </button>
        <p id="gfg"></p>
   <script>
      var el_down = document.getElementById("gfg");
      function gfg_Run() {
      const isIOS = ["iPad Simulator", 
                     "iPhone Simulator", 
                     "iPod Simulator", 
                     "iPad", "iPhone", 
                     "iPod"]
          .indexOf(navigator.platform) !== -1;
           el_down.innerHTML = isIOS;
            }
        </script>
    </body>
</html>

Approach 2: Use navigator.platform property to check for the particular keywords from the list Which belongs to iOS devices. Use pop() method to take out one by one and compare them.

<!DOCTYPE html>
<html>
    <head>
        <title>
            Detect whether a device is 
            iOS without using RegExp
        </title>
    </head>
    <body style="text-align: center;">
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
        <p>
            Click on the button to detect 
            whether a device is iOS or not?
        </p>
        <button onclick="gfg_Run()">
            Click Here
        </button>
        <p id="gfg"></p>
  <script>
    var el_down = document.getElementById("gfg");
    function iOS() {
        var Devices = ["iPad Simulator", 
                       "iPhone Simulator", 
                       "iPod Simulator", 
                       "iPad", "iPhone",
                       "iPod"];
         if (!!navigator.platform) {
            while (Devices.length) {
              if (navigator.platform === Devices.pop()) {
                            return true;
                        }
                    }
                }
                return false;
            }
            function gfg_Run() {
                el_down.innerHTML = iOS();
            }
        </script>
    </body>
</html>

Leave a comment

Your email address will not be published. Required fields are marked *