Javascript 用于重定向手机用户的Javascript代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6984541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript code to redirect mobile phone users
提问by webmasters
I have a javascript code to redirect mobile phone users. Could you check the code, I've picked it up from the net, total newb and I'd value your input regarding the quality of the script...
我有一个 javascript 代码来重定向手机用户。你能检查一下代码吗,我是从网上捡到的,完全是新手,我很重视你对脚本质量的投入......
Does it cover all types of mobile phones?
它涵盖所有类型的手机吗?
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");
回答by Rick Smalls
I ran the code below (with the obvious slight modifications) on my site, it seems to work pretty good with Androids, iPads, iPhones and Blackberries. I am using a PHP verison for my PHP pages and this code for my CGI/PERL pages, both with the same effect.
我在我的网站上运行了下面的代码(有明显的轻微修改),它似乎在 Android、iPad、iPhone 和黑莓上运行得很好。我为我的 PHP 页面使用了 PHP 版本,而我的 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>
回答by William Niu
Depending what you mean by "all the phones", but it doesn't cover, for example, blackberry phones, or tablets.
取决于您所说的“所有手机”是什么意思,但它不包括例如黑莓手机或平板电脑。
A better approach would be to detect the screen resolution, e.g. in jQuery you could do:
更好的方法是检测屏幕分辨率,例如在 jQuery 中,您可以执行以下操作:
if ( (screen.width < 1024) && (screen.height < 768) ) {
window.location = 'http://mobile.site.com';
}
回答by Michael Lynn
With the advent of rich resolution based mobile devices, it's probably not a good idea to leverage screen width. You could leverage a system written in server side technology such as PHP or CGI. There's a good tutorial over at http://www.mlynn.org/2010/06/mobile-device-detection-and-redirection-with-php/that includes downloadable examples and scripts to help with your implementation.
随着基于丰富分辨率的移动设备的出现,利用屏幕宽度可能不是一个好主意。您可以利用用服务器端技术(如 PHP 或 CGI)编写的系统。http://www.mlynn.org/2010/06/mobile-device-detection-and-redirection-with-php/上有一个很好的教程,其中包括可下载的示例和脚本,可帮助您实现。
回答by ShankarSangoli
I think it covers all the phones. But would like to provide few simple ways to clean the code.
我认为它涵盖了所有手机。但是想提供一些简单的方法来清理代码。
function IsSmartphone(){
return (DetectUagent("android") || DetectUagent("ipod") || DetectUagent("ipod") || DetectUagent("symbian"));
}
function DetectUagent(name){
return (navigator.userAgent.toLowerCase().search(name) > -1);
}