javascript “查看完整站点”移动站点选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5844966/
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
"VIEW FULL SITE" mobile site option
提问by somdow
So I'm working on the mobile version of a site I'm doing, and so far, I'm pulling the mobile sites content from its main counterpart, the main site.
所以我正在开发我正在做的网站的移动版本,到目前为止,我正在从其主要对应物,主网站中提取移动网站的内容。
As I study some mobile sites out there, I notice a lot of em have a "view full site" link.
当我研究一些移动网站时,我注意到很多网站都有“查看完整网站”链接。
Now I plan on redirecting the mobile visitors via .js in the header tag on main site via a check for screen width etc...(not sure if its the best way but so far the easiest on my brain))(but suggestions also welcome) but something like this
现在我计划通过检查屏幕宽度等在主站点的标题标签中通过 .js 重定向移动访问者......(不确定这是否是最好的方法,但到目前为止我脑子里最简单的方法))(但也有建议欢迎)但类似这样的
if (screen.width<=XyZ||screen.height<=XyZ) //example iphone size lets say 320x480
window.location.replace("mobile site link here.")
Again I dont know if this is the best way but, on dummy tests, it works on iPhone, some friends Droids, and one Blackberry. But it works.
同样,我不知道这是否是最好的方法,但在虚拟测试中,它适用于 iPhone、一些朋友 Droids 和一个黑莓。但它有效。
Anyways, so my question is, if i do this check on every page...how can I possible have a "view full site" option?
无论如何,所以我的问题是,如果我在每个页面上都进行此检查......我怎么可能有“查看完整站点”选项?
回答by Ming-Tang
Use PHP to detect mobile users through $_SERVER['HTTP_USER_AGENT']
.
JavaScript detection may not be reliable, because many mobile browsers do not support JS.
A "View Full Site" will set a cookie to reject mobile site, which is detectable.
Use cookies to keep track of your user's preferences.
使用 PHP 通过$_SERVER['HTTP_USER_AGENT']
. JavaScript 检测可能不可靠,因为许多移动浏览器不支持 JS。“查看完整站点”将设置 cookie 以拒绝可检测到的移动站点。使用 cookie 来跟踪用户的偏好。
In skeleton
在骨架中
<?php
if (isset($_COOKIE['nomobile'])) {
$style = "normal";
} else {
if (preg_match('/iPhone|(...etc...)/', $_SERVER['HTTP_USER_AGENT'])) {
$style = "mobile";
} else {
$style = "normal";
}
}
For the "View Full Site" page:
对于“查看完整站点”页面:
<a href="fullsite.php">Full Site</a>
fullsite.php
fullsite.php
<?php
setcookie('nomobile', 'true');
header('Location: index.php');
?>
回答by Matt
First, go to the following URL and download the mobile_detect.php file:
首先,转到以下 URL 并下载 mobile_detect.php 文件:
http://code.google.com/p/php-mobile-detect/
http://code.google.com/p/php-mobile-detect/
Next, follow the instructions on the page, and upload the mobile_detect.php to your root directory, Insert the following code on your index or home page:
接下来,按照页面上的说明,将 mobile_detect.php 上传到您的根目录,在您的索引或主页上插入以下代码:
<?php
@include("Mobile_Detect.php");
$detect = new Mobile_Detect();
if ($detect->isMobile() && isset($_COOKIE['mobile']))
{
$detect = "false";
}
elseif ($detect->isMobile())
{
header("Location:http://www.yourmobiledirectory.com");
}
?>
You will notice that the above code is checking for a cookie called "mobile", this cookie is set when the mobile device is redirected to the mobile page. To set the cookie insert the following code on your mobile landing page:
您会注意到上面的代码正在检查一个名为“移动”的 cookie,当移动设备被重定向到移动页面时,这个 cookie 被设置。要设置 cookie,请在您的移动登录页面上插入以下代码:
<?php
setcookie("mobile","m", time()+3600, "/");
?>
View the full article at: http://www.squidoo.com/php-mobile-redirect
回答by OZ_
It's not a best way, because very often JS aren't supported by mobile browsers.
这不是最好的方法,因为移动浏览器通常不支持 JS。
You can use this function:
您可以使用此功能:
function its_mobile_browser($user_agent = '')
{
if (empty($user_agent))
{
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (empty($user_agent)) return false;
}
if (stripos($user_agent, 'Explorer')!==false ||
stripos($user_agent, 'Windows')!==false ||
stripos($user_agent, 'Win NT')!==false ||
stripos($user_agent, 'FireFox')!==false ||
stripos($user_agent, 'linux')!==false ||
stripos($user_agent, 'unix')!==false ||
stripos($user_agent, 'Macintosh')!==false
)
{
if (!(stripos($user_agent, 'Opera Mini')!==false
|| stripos($user_agent, 'WAP')!==false
|| stripos($user_agent, 'Mobile')!==false
|| stripos($user_agent, 'Symbian')!==false
|| stripos($user_agent, 'NetFront')!==false
|| stripos($user_agent, ' PPC')!==false
|| stripos($user_agent, 'iPhone')!==false
|| stripos($user_agent, 'Android')!==false
|| stripos($user_agent, 'Nokia')!==false
|| stripos($user_agent, 'Samsung')!==false
|| stripos($user_agent, 'SonyEricsson')!==false
|| stripos($user_agent, 'LG')!==false
|| stripos($user_agent, 'Obigo')!==false
|| stripos($user_agent, 'SEC-SGHX')!==false
|| stripos($user_agent, 'Fly')!==false
|| stripos($user_agent, 'MOT-')!==false
|| stripos($user_agent, 'Motorola')!==false
)
) return false;
}
return true;
}
Or something better, lol :)
或者更好的东西,哈哈:)
回答by neebz
You can add a query string parameter to your website address such as ?fullsite=true
and include the following in your if condition >
您可以将查询字符串参数添加到您的网站地址,例如?fullsite=true
并在您的 if 条件中包含以下内容 >
var fullsite = getQueryString()["fullsite"];
if (fullsite != "true" && (screen.height <= xyz || screen.width <= abc)) //now redirect
You'll need the following function access query string. I took it from here > JavaScript query string
您将需要以下函数访问查询字符串。我从这里取的 > JavaScript 查询字符串
function getQueryString() {
var result = {}, queryString = location.search.substring(1),
re = /([^&=]+)=([^&]*)/g, m;
while (m = re.exec(queryString)) {
result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
return result;
}
And in the link you can have >
在链接中,您可以拥有 >
<a href="mysite.com?fullsite=true"> Show me Full Site </a>
<a href="mysite.com?fullsite=true"> Show me Full Site </a>
===========
============
Saying that please take a look at CSS Media Queries. It may require changing a bit of your design architecture but it's pretty useful.
说那请看一看 CSS Media Queries。它可能需要对您的设计架构进行一些更改,但它非常有用。
回答by ampersand
Server-side detection is definitely the way to do this, as you have no guarantee of JS being available or even turned on. A great PHP script for mobile detection is found here http://detectmobilebrowsers.mobi/and it gets a lot of use around the web.
服务器端检测绝对是做到这一点的方法,因为您无法保证 JS 可用甚至打开。在http://detectmobilebrowsers.mobi/ 上可以找到一个很棒的用于移动检测的 PHP 脚本,它在网络上得到了大量使用。