![](/img/trans.png)
[英]How to detect device is mobile when the website is clicked through TikTok?
[英]How to best detect if my website is being accessed through mobile device
我有一个基于python的网站(在app-engine上)。 这意味着我的服务器是用python编写的,并且我正在使用通常的webapp2和jinja2帮助服务我的页面。 我要完成的工作取决于是否有人通过iPhone,Android或普通计算机(即台式机或笔记本电脑)访问我的网站,以适当的“格式”来管理网页。 我以为有很多方法可以做到这一点
我在2
意思是
class ServeMyPage(webapp2.RequestHandler):
def get(self):
//...do some work to determine the device, such as screen size
if device==iPhone
//...get the iPhone page and serve it
self.response.out.write(iPhonePage);
else ...// etc, etc
在我的代码段中,我正在投放网页之前这样做。 我想这会更快,因为它不等待页面加载,然后检查一些JavaScript以查看是否应将内容更改为适当的格式。 我希望这很清楚。
那么最好的方法是什么? 我应该用JavaScript做吗? 其他方式? 还是如上所示? 如果如上所示,代码将是什么样? 我的问题也是技术性的,因为我不知道要在Python中放置什么代码。 谢谢。
您可以使用modernizr的触摸检测来检查它是否是触摸设备,如果是,则只有一个移动点。
if (Modernizr.touch) {
}
else
{
code for non mobile device
}
然后,您可以检查用户代理(这可能不是您的最佳选择,因为用户代理并不总是可靠的),并编写一个if语句,仅在else的非移动设备上执行该代码
var isMobile = {
Android: function () {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function () {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function () {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function () {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function () {
return navigator.userAgent.match(/IEMobile/i);
},
any: function () {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
if (isMobile.any()) {
}
else {
not mobile
}
另一种测试方法是通过检查window.innerWidth并仅在屏幕尺寸大于760px时才初始化脚本:
if (window.innerWidth > 760) {
not mobile
}
做到这一点的一种好方法是将上述3条语句结合起来进行检查。
您可以使用WURFL API来检测设备类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.