简体   繁体   English

重定向到移动网站

[英]Redirecting to the mobile website

I have coded few lines of java script code. 我已经编写了几行Java脚本代码。 But I am not sure that it is correct or not. 但是我不确定它是否正确。 Few months back I published a desktop version website. 几个月前,我发布了一个桌面版本的网站。 but when a user coming through a mobile (like android, iPhone or may be windows phone), I want to redirect a new path. 但是当用户通过移动设备(例如android,iPhone或Windows Phone)访问时,我想重定向一条新路径。 Here is the code: 这是代码:

// JavaScript Document
var uAgent = navigator.userAgent.toLowerCase();

if(uAgent.indexOf("android") > -1 || uAgent.indexOf("iphone") > -1 || uAgent.indexOf("windows phone") > -1) {
    window.location = "http://website path will come here.";
}

Is it the correct code? 这是正确的代码吗?

If you really need to detect a mobile browser (instead of making a mobile-friendly responsive layout like the Responsive template on Initializr that Just Works on mobile browsers - Google for "mobile first" and "responsive design" for more examples) then take a look at these links: 如果你真的需要检测移动浏览器(而不是使移动友好的响应式布局,如响应模板Initializr是在移动浏览器只是工程-谷歌为“移动第一”和“响应式设计”为更多的例子),然后取查看以下链接:

There are tons of mobile browsers - not only iPhone, Android and Windows - so if you are going to detect them then you need to do it right. 大量的移动浏览器-不仅是iPhone,Android和Windows-因此,如果要检测到它们,则需要正确执行。 See also the jQuery Mobile , a great mobile framework that will make your life easier. 另请参阅jQuery Mobile ,这是一个很棒的移动框架,可以使您的生活更轻松。

That will work, but its not a great way to do this for a couple of reasons: 这将起作用,但是由于以下几个原因,它并不是一个好方法:

  1. Users with JavaScript disabled will not get redirected 禁用JavaScript的用户将不会被重定向

  2. Mobile users (some with slow browsers and tight bandwidth limits) will download your desktop page before being redirected to the mobile site. 移动用户(某些浏览器速度慢且带宽限制严格)将在重定向到移动站点之前下载您的桌面页面。

A better approach if possible, would be to manage this via the web server configuration ideally without any redirects at all. 如果可能的话,更好的方法是通过Web服务器配置进行管理,理想情况下根本不需要任何重定向。 Alternatively, consider serving the same content to all users and changing the layout with media queries. 或者,考虑向所有用户提供相同的内容,并通过媒体查询更改布局。

uAgent.indexOf("android") > -1

Android is not just an operating system for smartphones. Android不仅是智能手机的操作系统。 It also runs on a lot of tablets which can easily handle the desktop-optimized versions of most websites. 它还可以在许多平板电脑上运行,这些平板电脑可以轻松处理大多数网站的桌面优化版本。 A friend of mine just complained that some websites only serve the mobile version to his GalaxyTab, even though the normal version would look well on his 10.1" screen. 我的一个朋友刚刚抱怨说,即使在他的10.1英寸屏幕上看起来正常的版本,某些网站也只能将移动版本提供给他的GalaxyTab。

You also forgot about blackberry and symbian phones. 您还忘记了黑莓和Symbian手机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM