rory-h rory-h - 2 months ago 13
Android Question

Dynamically change CTA button links (when on iOS or Android or web page)?

I have this button below which on desktop - when the user clicks it, it will bring them to a certain section as below. But how can I change the link to of the button so let's say - user is using an android phone the link of this button will change to google play store and when on IOS it will change to apple store?

<div class="get_the_app">
<a href="#get_app">get the app</a>
</div>

Answer

It is pretty easy try this

<div class="get_the_app">
           <a id="store" href="#get_app">get the app</a>
       </div>

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");
}

I have included windows store as well

Comments