简体   繁体   中英

Detect if device is iOS or Android, and then redirect

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? :)

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';
    }
}

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 ';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM