重定向手机用户的Javascript代码

重定向手机用户的Javascript代码,javascript,jquery,redirect,Javascript,Jquery,Redirect,我有一个javascript代码来重定向手机用户。你能检查一下代码吗,我已经从网上找到了,total newb,我会重视你关于脚本质量的输入 它包括所有类型的手机吗 function RedirectSmartphone(url) { if (url && url.length > 0 && IsSmartphone()) window.location = url; } function IsSmartphone() {

我有一个javascript代码来重定向手机用户。你能检查一下代码吗,我已经从网上找到了,total newb,我会重视你关于脚本质量的输入


它包括所有类型的手机吗

function RedirectSmartphone(url) {
    if (url && url.length > 0 && IsSmartphone())
        window.location = url;
}

function IsSmartphone() {
    if (DetectUagent("android")) return true;
    else if (DetectUagent("iphone")) return true;
    else if (DetectUagent("ipod")) return true;
    else if (DetectUagent("symbian")) return true;
    return false;
}

function DetectUagent(name) {
    var uagent = navigator.userAgent.toLowerCase();
    if (uagent.search(name) > -1)
        return true;
    else
        return false;
}
RedirectSmartphone("http://mobile.version.com");

我想它涵盖了所有的电话。但是我想提供一些简单的方法来清理代码

function IsSmartphone(){
    return (DetectUagent("android") || DetectUagent("ipod") || DetectUagent("ipod") || DetectUagent("symbian"));
}

function DetectUagent(name){
    return (navigator.userAgent.toLowerCase().search(name) > -1);
}

这取决于你所说的所有手机,但不包括黑莓手机或平板电脑

更好的方法是检测屏幕分辨率,例如,在jQuery中,您可以执行以下操作:

if ( (screen.width < 1024) && (screen.height < 768) ) { 
window.location = 'http://mobile.site.com';
} 

随着基于高分辨率的移动设备的出现,利用屏幕宽度可能不是一个好主意。您可以利用用服务器端技术(如PHP或CGI)编写的系统。这里有一个很好的教程,其中包括可下载的示例和脚本,帮助您实现。

我在我的网站上运行了下面的代码,并进行了明显的轻微修改,它似乎对Androids、iPad、iPhone和BlackBerry都很好。我在PHP页面上使用了PHP verison,在CGI/PERL页面上使用了这段代码,两者效果相同

<script type="text/javascript">
function RedirectSmartphone(url){
    if (url && url.length > 0 && IsSmartphone())
    window.location = url;
}
function IsSmartphone(){
    if (DetectUagent("android")) return true;
    else if (DetectUagent("blackberry")) return true;
    else if (DetectUagent("iphone")) return true;
    else if (DetectUagent("opera")) return true;
    else if (DetectUagent("palm")) return true;
    else if (DetectUagent("windows")) return true;
    else if (DetectUagent("generic")) return true;
    else if (DetectUagent("ipad")) return true;
    else if (DetectUagent("ipod")) return true;
    return false;
}
function DetectUagent(name){
    var uagent = navigator.userAgent.toLowerCase();
    if (uagent.search(name) > -1)
    return true;
    else
    return false;
}
RedirectSmartphone("http://mobile.version.com");
</script> 

如果需要,将为您生成代码。我以前用过他们的代码,效果很好。我不知道。我个人会检查屏幕分辨率,而不是嗅探用户代理。这样你就有了适合不同屏幕的正确页面/样式。它是否涵盖所有类型的手机?不。黑莓、WebOS和Windows Phone 7等等呢?这看起来很像是那些讨厌的网站之一。点击一篇文章的链接,重定向到手机主页脚本。@Quentin-我讨厌这些网站-我立即离开,再也不回来了。我的手机有一个功能强大的浏览器,我知道如何缩放。别装聋作哑,看起来干净多了。。。我应该使用它吗?它可靠吗?我想确定,因为我的一些收入将取决于您正在寻找的所有设备。这将给你100%的信心:我希望它涵盖平板电脑、黑莓平板电脑?!一个网站的移动版本总是在10屏幕上显示。Ty非常适合代码,分辨率更高。你能用重定向到lets say来编辑代码吗?看起来你已经这样做了…:在您的移动站点中,您仍然应该检查支持的不同移动屏幕大小,并应用相关的样式集。