Open In App

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

Last Updated : 28 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:




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

    
    

  • 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:




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

    
    

  • Output:


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads