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? :)
Asked
Active
Viewed 1.3k times
6
-
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 Answers
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
-
-
Yes, and you can redirect the user with window.location.replace("some link"); or window.location.href = "some link"; – Alex C Aug 19 '18 at 13:38
-
Do I need to add anything else to the html file? Can you also show if the code if it is a computer and not device? – Ian Swendee Aug 19 '18 at 19: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
-
-
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