将移动用户重定向到应用下载一次

时间:2014-11-12 00:27:22

标签: javascript php android cookies laravel

所以大多数时候,当我浏览网络时,如果我去一个有应用程序的网站,它首先会把我带到一个登陆页面,说明该网站在应用程序下载时更好,或者#34不,谢谢#34;然后,在你这样做后,它不再问你这个问题。实施这种行动的最简单方法是什么?使用javascript我可以找到一个人是否在Android上

var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {
    // Do something!
    // Redirect to Android-site?
    window.location = 'http://android.davidwalsh.name';
}

但是,我怎么才能让它只发生一次?我会将它添加到哪个页面?有任何想法吗?我在想cookie,但我不确定移动浏览器是否允许你以相同的方式添加cookie。

1 个答案:

答案 0 :(得分:1)

您可以在第一次访问时设置Cookie。在下次访问时,如果您检测到cookie,您可以立即重定向它们。如下所示(未经测试):

var ua = navigator.userAgent.toLowerCase();
    var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");

    if(isAndroid) {

        if (document.cookie.indexOf("mobile") >= 0) {

             //user has visited already on mobile so redirect immediately 
              window.location = 'http://android.davidwalsh.name';

           } else {

             expiry = new Date();
             expiry.setTime(date.getTime()+1000000); 

             //First time here - show a message, set a cookie and redirect etc.
             document.cookie = "mobile=yes; expires=" + expiry.toGMTString();
             window.location = 'http://android.davidwalsh.name';

            }

        }