我的目标是拥有一个基于 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/