dongyu9263 2013-06-16 03:07
浏览 22
已采纳

如何通过像素和js检测将用户重定向到正确的网站模板?

i created a nice code in jquery, it checks for pixels on screen and will soon redirect to the appropiate version

but i got to the point where i needed to support the disabled javascript and i got stuck at "how am i going to check if javascript is disabled in javascript?" silly right

so what am i supposed to do to run checks i heard of the viewport but i dont know if it can actually redirect to the index

var w = $(this).width();
        var h = $(this).height();
        if (w > 310 && w > 190 && h > 310)
        {
                alert('Tiny basic html Version <310x<310');

        }
        else if (w > 310 && w <= 800 && h > 480 && h < 1800)
        {// only if its not a desktop like windows or mac or linux or any other mature browser
        //only mobile detection here and other stuff
                alert('SmartPhone Version <800x>480');

        }
        if (w > 800 && w < 3500 && h < 3000)
        {//if its mobile os redirect to the smarphone version no matter how big that tablet is unless option

            alert('Large Desktop with OS detection');

        }
  • 写回答

1条回答 默认 最新

  • doujia1679 2013-06-16 03:12
    关注

    You can do a redirect to a mobile page

    <script type="text/javascript">
    <!--
    if (screen.width <= 700) {
    document.location = "/mobile";
    }
    //-->
    </script>
    

    But then, if JavaScript is disabled you can use css to re size and reshape the site, depending on the screen size

    @media screen and (max-width:800px) {
    
    // then put your mobile css in here
    
    }
    

    Same thing, but specific to a screen ratio (iphone 5)

    @media screen and (device-aspect-ratio: 40/71)
    {
    
    }
    

    more info here: http://css-tricks.com/snippets/css/media-queries-for-standard-devices/

    For @media screen it literally changes the page if you re size it there on the spot, you can remove columns or move them underneath others for every different size

    or if you really want to, use php for specific devices

    <?php
    $iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
    $android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
    $palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
    $berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
    $ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");
    // ect ect....
    
    if ($iphone || $android || $palmpre || $ipod || $berry == true) 
    { 
    header('Location: http://mobile.site.com/');
    //OR
    echo "<script>window.location='http://mobile.site.com'</script>";
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭