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

标签 php javascript jquery html css

我用 jquery 创建了一个很好的代码, 它检查屏幕上的像素并很快重定向到适当的版本

但我到了需要支持禁用的 javascript 的地步 我被困在 “我如何检查 javascript 中是否禁用了 javascript?”傻对吧

所以我应该怎么做才能运行检查我听说过视口(viewport)但我不知道它是否真的可以重定向到索引

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');

        }

最佳答案

您可以重定向到移动页面

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

但是,如果 JavaScript 被禁用,您可以使用 css 来调整网站的大小和形状,具体取决于屏幕大小

@media screen and (max-width:800px) {

// then put your mobile css in here

}

同样的事情,但特定于屏幕比例(iphone 5)

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

}

更多信息在这里:http://css-tricks.com/snippets/css/media-queries-for-standard-devices/

对于 @media screen,如果您在现场调整页面大小,它会从字面上改变页面,您可以删除列或将它们移动到其他列下方以获得不同的大小

或者如果你真的想要,为特定设备使用 php

<?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>";
}
?>

关于php - 如何通过像素和js检测将用户重定向到正确的网站模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17129817/

相关文章:

php - 为较大的句子分配标签?

php - 远程 Ratchet 连接

javascript选择当前div

javascript - 如何使用 Javascript 设置 CodeMirror 编辑器的值?

javascript - 通过点击标签调用 jQuery 模型

javascript - 使用数据从 jQuery 重定向

php - 基本 PHP 帮助 - 列数与第 1 行的值数不匹配

javascript - 带毫秒的 JQuery 倒数计时器

jquery - 如果我设置 float left 高度不起作用

php - 如何在不重新加载页面的情况下保持 session 事件?