这是我第一次开发响应式网站,我尝试使用CodeIgniter user_agent类。
我注意到有
is_mobile()和
is_browser()然而,我脑海中的图景是让平板电脑上的网站看起来与浏览器非常相似,只有移动网站才能完全加载不同的view文件。
但是,is_mobile()同时包含平板电脑和移动电话,这并不是我所希望的。有没有别的选择呢?
原因:我使用的是jQuery手机,我为手机加载了一个完全不同的布局,我不希望这个视图出现在平板电脑上。
发布于 2013-04-19 01:19:19
您有几个选择。
您可以扩展库并创建一个方法来检查tablet:
class MY_User_agent extends CI_User_agent {
public function __construct()
{
parent::__construct();
}
public function is_tablet()
{
//logic to check for tablet
}
}
// usage
$this->load->library('user_agent');
$this->user_agent->is_tablet();或者,您可以覆盖库中现有的is_mobile()方法,以获得所需的功能:
class MY_User_agent extends CI_User_agent {
public function __construct()
{
parent::__construct();
}
public function is_mobile()
{
// you can copy the original method here and modify it to your needs
}
}
// usage
$this->load->library('user_agent');
$this->user_agent->is_mobile();https://www.codeigniter.com/user_guide/general/creating_libraries.html
示例
application/libraries/MY_User_agent.php:
class MY_User_agent extends CI_User_agent {
public function __construct()
{
parent::__construct();
}
public function is_ipad()
{
return (bool) strpos($_SERVER['HTTP_USER_AGENT'],'iPad');
// can add other checks for other tablets
}
}控制器:
public function index()
{
$this->load->library('user_agent');
($this->agent->is_ipad() === TRUE) ? $is_ipad = "Yes" : $is_ipad = "No";
echo "Using ipad: $is_ipad";
}https://stackoverflow.com/questions/16089200
复制相似问题