【问题标题】:Redirect mobile users to app download once将移动用户重定向到应用程序下载一次
【发布时间】:2015-01-08 17:25:19
【问题描述】:

所以大多数时候,当我浏览网络时,如果我访问一个有应用程序的网站,它首先会将我带到一个登陆页面,上面写着 bla bla 该网站在应用程序上更好,请在此处下载或“不,谢谢”。然后在你这样做之后,它不再问你那个问题。实施这种行动的最简单方法是什么?使用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。

【问题讨论】:

    标签: javascript php android cookies laravel


    【解决方案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';
    
                }
    
            }
    

    【讨论】:

      猜你喜欢
      • 2012-08-19
      • 2012-04-20
      • 1970-01-01
      • 2019-11-25
      • 2023-01-24
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      相关资源
      最近更新 更多