我不确定我是否问了这个正确的方法。让我解释一下..。我注意到facebook是“没有”响应的网页,因为当我改变笔记本电脑的浏览器宽度时,facebook.com不会改变。另一方面,当我使用我的手机并在浏览器中运行时,facebook.com已经知道我使用的是移动设备,它的大小适合我的设备。
问:它是如何工作的,是某种优化我们的网站的伎俩吗?
发布于 2014-11-14 23:33:26
我不知道具体的Facebook,但如果没有迹象发现与JavaScript或CSS媒体查询,那么他们很可能是老派的方式,即与用户代理字符串。
每当您向网站提出请求时,浏览器都会在请求头中发送一个字符串,在我的示例中是:
User-Agent: Mozilla/5.0 (X11; Linux i686; rv:32.0) Gecko/20100101 Firefox/32.0 Iceweasel/32.0a2接收请求的服务器可以使用该字符串来了解您正在使用的操作系统和浏览器,您可以看到在我的示例中,操作系统是Debian,浏览器是Iceweasel (=firefox),您可以确定我在桌面计算机上浏览。
发布于 2014-11-14 23:50:02
我相信用户代理将是您的最佳选择。
navigator.userAgent发布于 2014-11-15 00:19:59
当我们在桌面上浏览时,服务器检测到我们在桌面上,它只是发送非responsive.but的默认(桌面)索引页,如果我们使用其他客户端,例如平板电脑或移动设备,服务器检测到它是.so,它将web文档网重定向到fb的其他移动友好页面,或者它将更改css,因为它与桌面page.it中的css不一样,这意味着服务器检测到我们的设备,而browser.the是可以用来检测的此类php检测脚本之一:
//php technique
<?php
$agent = $_SERVER['HTTP_USER_AGENT']; // Put browser name into local variable
if (preg_match("/iPhone/", $agent)) {
header("location: iphone_home.html");
} else if (preg_match("/android/", $agent)) {
header("location: android_home.html");
}?>
//javascript technique
<script language="javascript" type="text/javascript">
var agent = navigator.userAgent.toLowerCase();
if (agent.indexOf('iphone') != -1) { // iPhone Device
// If it is an iPhone put specific code to run here for iPhone users
} else if (agent.indexOf('android') != -1) { // Google phones running Android OS
// If it is a Google phone put specific code to run here for Android users
}
</script>
//php technique to detect OS and browser of user
<?php
$agent = $_SERVER['HTTP_USER_AGENT'];
$browserArray = array(
'Windows Mobile' => 'IEMobile',
'Android Mobile' => 'Android',
'iPhone Mobile' => 'iPhone',
'Firefox' => 'Firefox',
'Google Chrome' => 'Chrome',
'Internet Explorer' => 'MSIE',
'Opera' => 'Opera',
'Safari' => 'Safari'
);
foreach ($browserArray as $k => $v) {
if (preg_match("/$v/", $agent)) {
break;
} else {
$k = "Browser Unknown";
}
}
$browser = $k;
$osArray = array(
'Windows 98' => '(Win98)|(Windows 98)',
'Windows 2000' => '(Windows 2000)|(Windows NT 5.0)',
'Windows ME' => 'Windows ME',
'Windows XP' => '(Windows XP)|(Windows NT 5.1)',
'Windows Vista' => 'Windows NT 6.0',
'Windows 7' => '(Windows NT 6.1)|(Windows NT 7.0)',
'Windows NT 4.0' => '(WinNT)|(Windows NT 4.0)|(WinNT4.0)|(Windows NT)',
'Linux' => '(X11)|(Linux)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)|(Mac OS)'
);
foreach ($osArray as $k => $v) {
if (preg_match("/$v/", $agent)) {
break;
} else {
$k = "Unknown OS";
}
}
$os = $k;
echo $agent;
echo "<h2>You are using: <em>$browser - $os</em></h2>";
?>https://stackoverflow.com/questions/26940538
复制相似问题