Javascript 如何基于浏览器提供不同的内容
我最近偶然发现了这个网站:,当我用Chrome打开网站时,在页脚处我可以看到以下图片: 在Firefox中,相同的网站显示不同的内容:Javascript 如何基于浏览器提供不同的内容,javascript,php,jquery,Javascript,Php,Jquery,我最近偶然发现了这个网站:,当我用Chrome打开网站时,在页脚处我可以看到以下图片: 在Firefox中,相同的网站显示不同的内容: 我想实现这一点,但我不知道我应该做什么或如何开始。有人能帮我吗?提前感谢。看到网站的代码后,看起来他们在使用某种浏览器嗅探方法。这可以通过获取客户端的用户代理字符串来完成。这是一种很差的方法,但大部分都有效,因为用户代理很容易伪造 对您来说,解决方案是: 服务器端使用PHP。 您需要使用$\u服务器['HTTP\u用户\u代理] 客户端使用JavaScrip
我想实现这一点,但我不知道我应该做什么或如何开始。有人能帮我吗?提前感谢。看到网站的代码后,看起来他们在使用某种浏览器嗅探方法。这可以通过获取客户端的用户代理字符串来完成。这是一种很差的方法,但大部分都有效,因为用户代理很容易伪造 对您来说,解决方案是:
您需要使用
$\u服务器['HTTP\u用户\u代理]
您需要使用
navigator.userAgent
<?php
header("Content-type: text/plain");
function getBrowser()
{
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = 'Unknown';
$platform = 'Unknown';
$version= "";
//First get the platform?
if (preg_match('/linux/i', $u_agent)) {
$platform = 'linux';
}
elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
$platform = 'mac';
}
elseif (preg_match('/windows|win32/i', $u_agent)) {
$platform = 'windows';
}
// Next get the name of the useragent yes seperately and for good reason
if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
{
$bname = 'Internet Explorer';
$ub = "MSIE";
}
elseif(preg_match('/Firefox/i',$u_agent))
{
$bname = 'Mozilla Firefox';
$ub = "Firefox";
}
elseif(preg_match('/Chrome/i',$u_agent))
{
$bname = 'Google Chrome';
$ub = "Chrome";
}
elseif(preg_match('/Safari/i',$u_agent))
{
$bname = 'Apple Safari';
$ub = "Safari";
}
elseif(preg_match('/Opera/i',$u_agent))
{
$bname = 'Opera';
$ub = "Opera";
}
elseif(preg_match('/Netscape/i',$u_agent))
{
$bname = 'Netscape';
$ub = "Netscape";
}
// finally get the correct version number
$known = array('Version', $ub, 'other');
$pattern = '#(?<browser>' . join('|', $known) .
')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_agent, $matches)) {
// we have no matching number just continue
}
// see how many we have
$i = count($matches['browser']);
if ($i != 1) {
//we will have two since we are not using 'other' argument yet
//see if version is before or after the name
if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
$version= $matches['version'][0];
}
else {
$version= $matches['version'][1];
}
}
else {
$version= $matches['version'][0];
}
// check if we have a number
if ($version==null || $version=="") {$version="?";}
return array(
'userAgent' => $u_agent,
'name' => $bname,
'version' => $version,
'platform' => $platform,
'pattern' => $pattern
);
}
// now try it
$ua = getBrowser();
$yourbrowser = "Your browser: " . $ua['name'] . " " . $ua['version'] . " on " .$ua['platform'] . " reports:\n" . $ua['userAgent'];
print_r($yourbrowser);
?>
Your browser: Google Chrome 47.0.2526.111 on windows reports:
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36
Your browser: Mozilla Firefox 42.0 on windows reports:
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0
Your browser: Unknown ? on windows reports:
Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; managedpc; rv:11.0) like Gecko
Your browser: Google Chrome 47.0.2526.111 on windows reports:
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36
Your browser: Mozilla Firefox 42.0 on windows reports:
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0
Your browser: Unknown ? on windows reports:
Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; managedpc; rv:11.0) like Gecko
最终更新 现在,基于上面的代码,您可以通过这种方式从服务器端跨不同的浏览器交付不同的内容
<?php
// The same function to be called.
$ua = getBrowser();
// Let's check for our required values.
if ($ua['name'] == "Mozilla Firefox")
// Add Firefox's content.
echo "firefox.png";
elseif ($ua['name'] == "Google Chrome")
// Add Chrome's content.
echo "chrome.png";
else
// Finally add our devastated IE's content.
echo "ie.png";
?>
看到网站的代码后,看起来他们在使用某种浏览器嗅探方法。这可以通过获取客户端的用户代理字符串来完成。这是一种很差的方法,但大部分都有效,因为用户代理很容易伪造。使用BOM中的navigator
对象。浏览器对象模型?字节顺序标记<代码>:P
你能解释一下吗?@nandhakumar当然,给你写一个答案<代码>:)PHP解决方案适合您吗?是的,我只使用PHP。PHP解决方案会很棒。@nandhakumar是的,这就是代码。最后一个。看看,这看起来很有希望。让我试着回来。@nandhakumar我肯定在等它<代码>:)