Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One button -- 3 App stores -- how to redirect the user to the appropriate appstore?

I am wanting to create a simple button which redirects mobile users to the appropriate app store link depending on which mobile os they are running (ios, android or wp8) - or if not on a mobile then offers to send an email containing the appropriate link...Any ideas?

like image 328
anthonyhumphreys Avatar asked Aug 08 '14 18:08

anthonyhumphreys


1 Answers

Well... This is actually pretty straightforward. To begin with, you must detect the device the user is currently using by means of their user agent string. And then use jQuery to simply set the href attribute of the anchor element correctly. The following code illustrates.

var operatingSystem, userAgentString = navigator.userAgent;
var link = $("#store");

if (userAgentString.indexOf("iPhone") > -1 || userAgentString.indexOf("iPod") > -1 || userAgentString.indexOf("iPad") > -1) {
    operatingSystem = "iOS";
    link.attr("href", "http://store.apple.com/us/browse/app");
} else if (/Android/.test(userAgentString)) {
    operatingSystem = "Android";
    link.attr("href", "https://play.google.com/store/apps?hl=en");
} else if (/Windows Phone/.test(userAgentString)) {
    operatingSystem = "Windows Phone";
    link.attr("href", "http://www.windowsphone.com/en-us/store");
}

http://jsfiddle.net/5g0zqm0s/

like image 81
Siddharth Avatar answered Oct 19 '22 19:10

Siddharth