6

So I have a website, and what I want is that when the open my website: www.example.com/download.html I want to first detect if the device is iOS device or Android device, and then redirect to another link, for example www.google.com (just an example). I want different link for different OS. Any tips on how I can manage this? :)

Ian Swendee
  • 61
  • 1
  • 2
  • 5
  • This question is similar: https://stackoverflow.com/q/6322112/7362396 – Tobias K. Aug 18 '18 at 19:34
  • Alternatively, a quick Google search comes up with a promising looking library if you don't want to put too much of the code in your source: https://github.com/WhichBrowser/Parser-PHP – Tobias K. Aug 18 '18 at 19:35

2 Answers2

5

You can use the user agent string to detect different kinds of devices like so:

function androidOrIOS() {
    const userAgent = navigator.userAgent;
    if(/android/i.test(userAgent)){
        return 'android';
    }
    if(/iPad|iPhone|iPod/i.test(userAgent)){
        return 'ios';
    }
}
Alex C
  • 516
  • 7
  • 15
1

You can use Navigator to determine the type of device

function navigate() {
    if((/Mobi|Android/i.test(navigator.userAgent))){
        window.location.href = 'android url ';
    }
    if(/Mobi|iPad|iPhone|iPod/i.test(navigator.userAgent)){
        window.location.href = 'ios url ';
    }
}
Madhan Varadhodiyil
  • 2,086
  • 1
  • 14
  • 20
  • how widely supported is Navigator? – ina Mar 31 '19 at 09:20
  • It looks to me like searching for "Mobi" in the second `if` is unnecessary, as the first one will already catch that case. Overall it would seem to me like this will not work correctly going by that alone. – damd Apr 15 '21 at 08:57