0

I need to create a common link that redirect to Android PlayStore or Apple App Store according to the os. This link will open in the browser.

Paras Dhawan
  • 304
  • 2
  • 19

1 Answers1

2

You can do that using javascript:

function changeLink(){
    document.getElementById('link').href= getMobileOperatingSystem();
}

function getMobileOperatingSystem() {
  var userAgent = navigator.userAgent || navigator.vendor || window.opera;

      // Windows Phone must come first because its UA also contains "Android"
    if (/windows phone/i.test(userAgent)) {
        return "windows-link";
    }

    if (/android/i.test(userAgent)) {
        return "android-link";
    }

    // iOS detection from: http://stackoverflow.com/a/9039885/177710
    if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
        return "ios-link";
    }

    return "ios-link"; // default
}
Niraj Chauhan
  • 6,765
  • 9
  • 41
  • 73