javascript - 检测手机或平板设备

标签 javascript wordpress device detection

我的目标是拥有一个基于 Wordpress 的移动网站(适用于手机和平板电脑)和一个响应式桌面网站。我想要最简单的方法来实现万无一失的设备检测。

移动网站将拥有许多仅真正有益于触摸设备的功能,并将针对手机和平板电脑进行定制设计。桌面站点将完全不同(具有相同的页面,但包含额外的内容)并且将完全响应以防万一任何设备漏过检测。

我有一个衬垫可以检测触摸设备并重定向到另一个页面,但它似乎太简单了,无法成为设备检测的万无一失的方法。这是多么愚蠢的证明?它适用于 Windows 和 Android 设备以及 iOS 设备吗?

这是我第一次做这样的网站,不确定效果如何:

<!-- Redirect to the mobile index page if we're on a touch device -->
<script>if( 'ontouchstart' in window ) window.location = 'mobile.html';</script>

最佳答案

您可以使用 mdetect (移动检测)库为您做这件事。它以多种语言编写,可以检测 Windows、Android、iOS 等。请务必阅读文档。

请注意,JavaScript 版本包含此警告:

// WARNING: 
//   These JavaScript-based device detection features may ONLY work 
//   for the newest generation of smartphones, such as the iPhone, 
//   Android and Palm WebOS devices.
//   These device detection features may NOT work for older smartphones 
//   which had poor support for JavaScript, including 
//   older BlackBerry, PalmOS, and Windows Mobile devices. 
//   Additionally, because JavaScript support is extremely poor among 
//   'feature phones', these features may not work at all on such devices.
//   For better results, consider using a server-based version of this code, 
//   such as Java, APS.NET, PHP, or Ruby.

关于javascript - 检测手机或平板设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14936105/

相关文章:

javascript - 评论的滑动/滚动效果

javascript - 了解基本原型(prototype)设计和更新键/值对

android - 无法通过 Android 设备进行调试

javascript - WebRTC:从音频输入设备中选择

mysql - 转移WordPress,尝试了一切

android BluetoothDevice.getName() 返回 null

javascript - lof JSliderNews 不起作用

javascript - Jquery的返回值由两个变化函数来计算

php - 希望将头文件导入我的子主题

Cloud9 IDE 上的 WordPress 重定向