25

I will send my clients the link for the app in the following format

http://goo.gl.com/downloadtheapp (or whatever)

I want this file be somewhere on my server that includes java script code that checks what is the device type and redirects to the convenient store. that is, google play if the device was android based and appstore if the device was ios based.

till now I tried this, but it does not work.

<html>
<head>
<script type="text/javascript">
        $(document).ready(function () 
    {
        if(navigator.userAgent.toLowerCase().indexOf("android") > -1)
        {
            window.location.href = 'http://play.google.com/store/apps/details?id=com.truecaller&hl=en';
        }
        if(navigator.userAgent.toLowerCase().indexOf("iphone") > -1)
        {
            window.location.href = 'http://itunes.apple.com/lb/app/truecaller-caller-id-number/id448142450?mt=8';
        }
    }
</javascript>
</head>
<body>
</body>
</html>
Govind Singh
  • 15,282
  • 14
  • 72
  • 106
tony9099
  • 4,567
  • 9
  • 44
  • 73

1 Answers1

48

The problem is with your script tag and you are missing );

And by the way, did you import jQuery?

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

<script>
    $(document).ready(function (){
        if(navigator.userAgent.toLowerCase().indexOf("android") > -1){
            window.location.href = 'http://play.google.com/store/apps/details?id=com.truecaller&hl=en';
        }
        if(navigator.userAgent.toLowerCase().indexOf("iphone") > -1){
            window.location.href = 'http://itunes.apple.com/lb/app/truecaller-caller-id-number/id448142450?mt=8';
        }
    });
</script>
cRAN
  • 195
  • 1
  • 2
  • 16
Govind Singh
  • 15,282
  • 14
  • 72
  • 106
  • 5
    For those who wind up here, I recommend looking at link redirection as a solution, as it generally is cheaper (resource-wise). I have no affiliation with this service, but http://onelink.to/ is a good example - or your own in-house link forwarding - this improves load time by not having to load any javascript (jquery etc.) – jdero Sep 07 '17 at 21:29
  • I would recommend to avoid unnecessary use of Jquery. Try to use: document.addEventListener at DOMContentLoaded instead. The strong reason to avoid using Jquery to do any simple task is to decrease time loading and network consumption. Jquery load all libs inside the script even the ones you will never use. Mobile users using mobile network will thank you for saving some bandwidth. – lcsvcn Jul 21 '20 at 16:33