PHP Mobile Detect加载单独的样式表

时间:2015-12-22 00:24:14

标签: php mobile

我使用找到here的脚本来加载样式表,具体取决于它是台式机,平板电脑还是手机。

我们的想法是桌面和平板电脑具有相同的样式表,但对于手机来说,要加载单独的样式表。

检测脚本是几百行(也许是几千行)所以对于它来说,它在链接中,但是我编写的用于加载单独样式的代码是:

include($_SERVER['DOCUMENT_ROOT'].'/php/mobiledetect.php');
$detect = new Mobile_Detect();

if($detect->isTablet()) {
   echo '<link rel="stylesheet" href="http://example.com/css/style.css">'."\r\n";
   echo '<link rel="stylesheet" href="http://example.com/css/notification.css">'."\r\n";
}
if ($detect->isMobile()) {
   echo '<link rel="stylesheet" href="http://example.com/css/m.style.css">'."\r\n";
   echo '<link rel="stylesheet" href="http://example.com/css/m.notification.css">'."\r\n";
}
else {
   echo '<link rel="stylesheet" href="http://example.com/css/style.css">'."\r\n";
   echo '<link rel="stylesheet" href="http://example.com/css/notification.css">'."\r\n";
}

出于某种原因,手机工作正常,但平板电脑正在加载样式表。

我尝试将电话线放在平板电脑之前,但手机开始加载普通样式表而不是手机样式,但平板电脑工作正常。

我想坚持使用PHP,因为我计划在检测中添加变量,以便更改其他项目,例如标题等。

1 个答案:

答案 0 :(得分:3)

如果因为平板电脑也被视为移动设备,您需要使用其他设备。试试这个:

include($_SERVER['DOCUMENT_ROOT'].'/php/mobiledetect.php');
$detect = new Mobile_Detect();

if($detect->isTablet()) {
   echo '<link rel="stylesheet" href="http://example.com/css/style.css">'."\r\n";
   echo '<link rel="stylesheet" href="http://example.com/css/notification.css">'."\r\n";
} else if ($detect->isMobile()) {
   echo '<link rel="stylesheet" href="http://example.com/css/m.style.css">'."\r\n";
   echo '<link rel="stylesheet" href="http://example.com/css/m.notification.css">'."\r\n";
} else {
   echo '<link rel="stylesheet" href="http://example.com/css/style.css">'."\r\n";
   echo '<link rel="stylesheet" href="http://example.com/css/notification.css">'."\r\n";
}
相关问题