zatu
zatu

Reputation: 53

Redirect to app store or google store after button click

I have a button on my PHP page, currently I'm using this javascript code to open the app store and play store, but this code will directly open the play store and app store page and not on the button click, I want it to open after the user click on the button and it will open based on the browser agent. Can anyone help me?

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

<script>
    $(document).ready(function (){
     if(navigator.userAgent.toLowerCase().indexOf("iphone") > -1){
         window.location.href = 'https://itunes.apple.com/my/app/flipbizz/idexampleapp';
     }
    });
</script>

<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 = 'https://play.google.com/store/apps/details?id=com.exampleapp';
     }
    });
</script>

  <p><button> JOIN ME NOW</button></p>
</div> 

</div>

Upvotes: 5

Views: 8489

Answers (1)

Nadun Kulatunge
Nadun Kulatunge

Reputation: 1731

Try this

<button onclick="myFunction()">Click me</button>


<script>
function myFunction() {
    if(navigator.userAgent.toLowerCase().indexOf("iphone") > -1){ window.location.href = 'https://itunes.apple.com/my/app/flipbizz/idexampleapp'; }

    if(navigator.userAgent.toLowerCase().indexOf("android") > -1){ window.location.href = 'https://play.google.com/store/apps/details?id=com.exampleapp'; }

    //Update #2
    if (!navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|IEMobile)/)) {
         window.location.href = 'https://www.google.com'; //Desktop Browser
    }
}
</script>

Upvotes: 6

Related Questions