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

The task is to detect whether the device is iOS or not without using RegExp with the help of JavaScript. There are two approaches that are discussed below.

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

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!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>

    chevron_right

    
    

  • Output:

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.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!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>

    chevron_right

    
    

  • Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.