点击按钮后重定向到应用商店或谷歌商店

5
我在PHP页面上有一个按钮,目前我正在使用这段javascript代码来打开应用商店和Play商店,但是这个代码会直接打开Play商店和应用商店页面而不是通过按钮点击的方式,我想要它在用户点击按钮后根据浏览器代理打开。有人能帮我吗?
 <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>

2
为按钮添加 onclick - Agney
这里使用jQuery非常聪明。您只需要两行代码就能完成20行的工作。太酷了。 - statosdotcom
你能更具体地说明应该在哪个部分添加吗?我是一个新手。 - zatu
1个回答

6
尝试这个。
<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>

很高兴为您服务。=) - Nadun Kulatunge
如果我想在桌面浏览器中打开它,我应该添加什么? - zatu
如果(!navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|IEMobile)/)){ window.location.href = 'https://www.google.com' } - Nadun Kulatunge

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接