有没有办法仅使用 Coldfusion 来检测用户是否在移动设备上?
- 我没有进行移动重定向。
- 我正在使用 CSS 媒体查询来更改布局以适应屏幕宽度。
- 我只想做一个简单的操作,如果移动设备呈现此信息,否则将此信息呈现到网站的部分而不是整个网站。 (例如:如果移动[显示jquery动画],否则显示[链接])
- 我不想浪费时间渲染两者,如果只是对无论如何都不会显示的信息进行显示。
- 没有 javascript 移动检测(人们可能在浏览器上关闭了 javascript)
Is there a way to detection if a user is on a mobile device using just coldfusion only?
- I'm not doing a mobile redirect.
- I am using CSS media queries to change the layout to fit screen width.
- I just want to do a simple if mobile render this information else render this information instead to parts of the site not the whole thing. (ex: if mobile [show jquery animation] else show [link])
- I don't want to waste time rendering both if only to do a display none to the info not going to be shown anyways.
- No javascript mobile detection (people might have javascript off on their browsers)
发布评论
评论(8)
我使用 http://detectmobilebrowsers.com/ 上的代码来执行此操作。他们使正则表达式保持最新状态。关于页面有关于平板电脑的详细信息(如果您需要的话)以及移动设备。删除 UDF 函数中的 if 以便轻松地使其与进一步的更改保持同步。
CFM版本:
I used the code up at http://detectmobilebrowsers.com/ to do this. They keep the regex pretty current. The about page there has details on tablets if you need that as well as mobile. Drop if in a UDF function in order to easily keep it up-to-date on further changes.
The CFM version:
我写了一篇关于这件事的博客文章!
http://www.mccran.co.uk/index.cfm/2010/6/4/Simple-Coldfusion-script-to-detect-if-a-user-is-on-a- Mobile-platform
您可以轻松地从用户代理字符串中检测浏览器。不过,正如我最近经历的那样,像 Chrome 这样的浏览器将允许您更改用户代理字符串:
http://www.mccran.co.uk/index.cfm/2011/9/25/Samsung-Galaxy-Tab-101-Switching-your-browser-from-mobile-view
I wrote a blog entry about this very thing!
http://www.mccran.co.uk/index.cfm/2010/6/4/Simple-Coldfusion-script-to-detect-if-a-user-is-on-a-Mobile-platform
You can easily detect the browser from the user agent string. Tho as I've experienced recently browsers like chrome will allow you to change your user agent string:
http://www.mccran.co.uk/index.cfm/2011/9/25/Samsung-Galaxy-Tab-101-Switching-your-browser-from-mobile-view
使用 CGI.HTTP_USER_AGENT:
将 MobileAgentOne 替换为您要查找的浏览器。
快速谷歌应该为您提供当前正在使用的移动代理的足够列表,例如:http ://www.useragentstring.com/pages/Mobile%20Browserlist/
如果您想变得更强大一点,这里有一种提供部分匹配的方法,这样您就可以寻找手机系列,而不是比列出来一百万个不同的独特移动代理:
Use CGI.HTTP_USER_AGENT:
replace MobileAgentOne with the browser you are looking for.
A quick google should provide you with an adequate list of Mobile agents currently in-use, such as: http://www.useragentstring.com/pages/Mobile%20Browserlist/
If you'd like to be a tad more robust, here's a way to provide partial matches, so you can look for families of mobile phones, rather than listing out one million different unique mobile agents:
您可以检查
CGI.HTTP_USER_AGENT
的值,看看它是否表明客户端是移动设备。以下是基于我找到的一些 Python 代码的快速片段,但您可能可以执行更高级的检查:
You can examine the value of
CGI.HTTP_USER_AGENT
and see if it indicates that the client is a mobile.Here's a quick snippet based on some Python code I found but there may be more advanced checks you can do:
(很抱歉回答这个问题,但我无法在这里发表评论。)
对字符串“android”的测试捕获了一堆平板电脑(motorola xoom、三星 Galaxy Tab、华硕 Transformer、其他来自东芝、索尼、宏基等的平板电脑) .)。 Android 是一个操作系统,而不是一类设备。
我想说,一个现实的答案很大程度上取决于您所认为的“移动”设备。可以说,考虑到许多平板电脑的处理能力和物理屏幕尺寸,许多平板电脑在“普通”(非移动)网站上表现得很好,因此将它们与更小、功能更弱的手机混在一起对这些用户来说是一个disservice。
请注意,之前的几个答案中列出了“iPhone”,但没有列出“iPad”,因此它们隐式排除了 iOS 平板电脑,我相信这对于 Android 来说也是正确的。
不幸的是,基于这种方法的答案并不那么简单。网上有各种解决方案,其中一个基本版本是查找“mobile”一词并排除一些特定于平板电脑的字符串,例如tablet、pad、xoom、eeepc、flyer等。但是,每周都会出现新设备,即使是追逐最流行的设备也将是一个挑战。
(Apologies for making this an answer, but I'm unable to comment here.)
Testing for the string 'android' catches a bunch of tablets (motorola xoom, samsung galaxy tab, asus transformer, others from toshiba, sony, acer, etc.). Android is an OS, not a class of devices.
I'd say a realistic answer depends a lot on what you consider to be a "mobile" device. Arguably, many tablets do just fine with "normal" (non-mobile) sites, given their processing power and physical screen size, so lumping them in with much smaller and less capable mobile phones does those users a disservice.
Note that 'iPhone' is listed in several previous answers, but not 'iPad', so they implicitly exclude iOS tablets, which I believe is correct for android too.
An answer based on that approach is much less trivial, unfortunately. Various solutions are out there on the net, one basic version of which is to look for the word mobile and exclude some tablet-specific strings such as tablet, pad, xoom, eeepc, flyer, etc. However, new devices appear every week, and chasing even the most popular ones will be a challenge at best.
detectormobilebrowsers.com 解决方案是否位于标题中?有什么理由不把检测线放在表头之前吗?我有一个框架集(我知道,不理想......),我认为框架集首先被检测到并在运行检测脚本运行之前加载我的框架。 (这可能吗?)
它在 android 的标题中工作正常,但在 kindle、ipad 或 iphone 上则不行(我添加了一条 ipad 行)。
感谢您的阅读,
LPP
Does the detectmobilebrowsers.com solution go in the header? Is there any reason not to put the detection line before the header? I have a frameset (I know, not ideal...) and I think the frameset is getting detected first and loading my frames before running the detection script runs. (Is that possible?)
It works fine in the header with android, but not kindle, ipad or iphone (I added an ipad line).
Thanks for reading,
LPP
(很抱歉回答而不是评论,我无法评论或投票)
我最初喜欢 mujimu 的解决方案。我把它放在我的网站上,用我自己的 iPhone 进行测试似乎效果很好。但是,我开始用这个 http://www.mobilephoneemulator.com/ 进行测试,但它不起作用大多数设备。
这就是我会做的(并且将会做的)。在一个谨慎的位置输出类似的内容
#CGI.HTTP_USER_AGENT#
。然后测试并记录设备的输出内容。然后,使用类似的东西(谢谢 Peter Boughton),并在管道之间,使用测试中的不同关键字...
经过我的初步测试,这是我列出的列表。如果您认为这是准确的,请告诉我...
ARCELIK、Access、Acer、Ahong、Aiko、Airis、Airness、Alcatel、AlphaCell Wireless、Amazon、Amoi、Amoisonic、Android、AnexTek、Apple、Archos、Asmobile、Aspen、华硕,华硕电脑INC.、Audiovox、Becker、BEKO、Bellawave、BellWave、BenQ、明基西门子、明基西门子、Bird、Bleu、Blue Coat、B-Mobile、Bolt、Bytemobile、首信、卡西欧、CDM、CECT、CheckCom、Cingular、Cking、仁宝、康柏、CoralWeb、Cricket、Dai电信、Dallab、Danger、大显、DBTEL、Dicam、Dmobo、DoCoMo、Dopod、Doris、DreamPhone、EDL、Elite、ELSON、Emblaze、Emobile、Enteos、Era、Ericsson、Ericy、ETEN、EZIO、EZZE、Fitel、Fly、富士通、通用、通用网络浏览器、通用 Windows、通用 Windows手机、技嘉、金立、Google、Gradiente、根德、海尔、Handspring、七喜、黑海、Helio、惠普、海信、日立、HP、HTC、HTC Corporation、HTIL、华为、悍马、IAC OKWAP、IAC OKWAP Mobile通信、Idea、iKoMo、IM、Imate、i-mate、i-mobile、英飞凌、InfoSonics、Innostream、INQ Mobile、Itelco、IXI、KCM、KDDI、KDDI -索尼爱立信、KDDI-卡西欧、KDDI-日立、KDDI-京瓷、KDDI-京瓷、KDDI-松下、KDDI-泛泰、KDDI-三洋、KDDI-夏尔p,KDDI-索尼爱立信,KDDI-索尼爱立信,KDDI-东芝,科健,凯森,克朗代克,康佳,科子,KPT,K-Touch,京瓷,京瓷Wireless Corp、LCT、联想、联想移动、Lexibook、雷克萨斯、LG、LG Electronics、LGE、Lobster、Longcos、LT、LXE、M3 Gate、Malata、基于 MAUI 的 Generic、Maxon、Medion、MicroMax、Microsoft、MIO、Mio科技、Miracle、神达、三菱、移动无线集团、MOBISTEL、Modelabs、ModelLabs、Modottel、Modu、MOMO设计、Morange、摩托罗拉、Myphone、NEC、Neonode、Newgen、Nexian、任天堂、诺基亚、Novarra、NTT DoCoMo、NTT DoCoMo FOMA 3G、O2、ONDA、Openwave、Opera、Optimay、Orange、Palm、PalmOne、松下、Panda、 Pantech、PCD、飞利浦、PhoneOne、倍耐力-Arcor、保时捷Design、Porshe、Poseidon、Psion、QCI、QMobile、Qtek、Raks、Reporo、Research In Motion Ltd.、RIM、Robot、Ron、Rover、RT、Sagem、SAMART、三星、三洋、SavaJe、SEC、Semc、Sendo、夏普、西门子、天火、Skyspring、Skyspring Vitelcom、创维、SKYZEN、SlobTrot软件、SmartTrust、Smile、软银、Sonim、索尼、索尼爱立信移动通信、索尼爱立信、Spice、Sprint、SPV、Sunrise、TCL、德信、Tel.Me.、意大利电信、泰利特、天宇、T-Mobile、Toplux、东芝,Tranxcode,TTPCom,tvCompass,tvCompass DMR1500、Ubiquam、UCWEB、Uniscope、Unistar、Uriver、Usha Lexus、Utec、UTStarcom、V3m (Metro PCS)、Vacom、Velocity Mobile、Venera、Verizon、Vertu、VeryKool、VERZIOWORLD、Vitelcom、VITELCOM for Telefonica Movistar、VK、VK移动,沃达丰,沃达丰中兴、Voxtel、W3C、Wapamp、Wapsilon、WAPUniverse、WellcoM、WinWAP Technologies、Wonu、XDA、Yas、Yospace 爱立信、Yospace 摩托罗拉、Yospace 诺基亚、Yospace 夏普、Yospace 西门子、Yospace 索尼爱立信、宇龙、中大、ZT、中兴、。手机
(Sorry for answering instead of commenting, I'm not able to comment or vote up)
I initially liked mujimu's solution. I put it on my site and it seemed to work well testing with my own iphone. However, I started testing with this http://www.mobilephoneemulator.com/ and it didn't work for a majority of devices.
Here's what I would do (and will do). Output something like this in a discreet location
<cfoutput>#CGI.HTTP_USER_AGENT#</cfoutput>
. Then test and record what the devices output.Then, use something like this (thank you Peter Boughton) and in between the pipes, use a distinct keyword from the testing...
After my initial testing here's the list I've come up with. Please let me know if you think this is accurate...
ARCELIK,Access,Acer,Ahong,Aiko,Airis,Airness,Alcatel,AlphaCell Wireless,Amazon,Amoi,Amoisonic,Android,AnexTek,Apple,Archos,Asmobile,Aspen,Asus,ASUSTeK COMPUTER INC.,Audiovox,Becker,BEKO,Bellawave,BellWave,BenQ,BenQSiemens,BenQ-Siemens,Bird,Bleu,Blue Coat,B-Mobile,Bolt,Bytemobile,Capitel,Casio,CDM,CECT,CheckCom,Cingular,Cking,Compal,Compaq,CoralWeb,Cricket,Dai Telecom,Dallab,Danger,Daxian,DBTEL,Dicam,Dmobo,DoCoMo,Dopod,Doris,DreamPhone,EDL,Elite,ELSON,Emblaze,Emobile,Enteos,Era,Ericsson,Ericy,ETEN,EZIO,EZZE,Fitel,Fly,Fujitsu,generic,generic web browser,Generic Windows,Generic Windows Mobile,GIGABYTE,Gionee,Google,Gradiente,Grundig,Haier,Handspring,Hedy,Hei,Helio,Hewlett-Packard,Hisense,Hitachi,HP,HTC,HTC Corporation,HTIL,HUAWEI,Hummer,IAC OKWAP,IAC OKWAP Mobile Communications,Idea,iKoMo,IM,Imate,i-mate,i-mobile,Infineon,InfoSonics,Innostream,INQ Mobile,Itelco,IXI,KCM,KDDI,KDDI - SonyEricsson,KDDI-Casio,KDDI-Hitachi,KDDI-Kiocera,KDDI-Kyocera,KDDI-Panasonic,KDDI-Pantech,KDDI-Sanyo,KDDI-Sharp,KDDI-SonyEriccson,KDDI-SonyEricsson,KDDI-Toshiba,Kejian,Kisen,Klondike,KONKA,Kozi,KPT,K-Touch,Kyocera,Kyocera Wireless Corp,LCT,Lenovo,Lenovo Mobile,Lexibook,Lexus,LG,LG Electronics,LGE,Lobster,Longcos,LT,LXE,M3 Gate,Malata,MAUI-based Generic,Maxon,Medion,MicroMax,Microsoft,MIO,Mio Technology,Miracle,MiTAC,Mitsubishi,Mobile Wireless Group,MOBISTEL,Modelabs,ModelLabs,Modottel,Modu,MOMO Design,Morange,Motorola,Myphone,NEC,Neonode,Newgen,Nexian,Nintendo,Nokia,Novarra,NTT DoCoMo,NTT DoCoMo FOMA 3G,O2,ONDA,Openwave,Opera,Optimay,Orange,Palm,PalmOne,Panasonic,Panda,Pantech,PCD,Philips,PhoneOne,Pirelli-Arcor,Porsche Design,Porshe,Poseidon,Psion,QCI,QMobile,Qtek,Raks,Reporo,Research In Motion Ltd.,RIM,Robot,Ron,Rover,RT,Sagem,SAMART,Samsung,Sanyo,SavaJe,SEC,Semc,Sendo,Sharp,Siemens,Skyfire,Skyspring,Skyspring Vitelcom,Skyworth,SKYZEN,SlobTrot Software,SmartTrust,Smile,SoftBank,Sonim,Sony,Sony Ericsson Mobile Communications,SonyEricsson,Spice,Sprint,SPV,Sunrise,TCL,Techfaith,Tel.Me.,Telecom Italia,Telit,TIANYU,T-Mobile,Toplux,Toshiba,Tranxcode,TTPCom,tvCompass,tvCompass DMR1500,Ubiquam,UCWEB,Uniscope,Unistar,Uriver,Usha Lexus,Utec,UTStarcom,V3m (Metro PCS),Vacom,Velocity Mobile,Venera,Verizon,Vertu,VeryKool,VERZIOWORLD,Vitelcom,VITELCOM for Telefonica Movistar,VK,VK Mobile,Vodafone,Vodafone ZTE,Voxtel,W3C,Wapamp,Wapsilon,WAPUniverse,WellcoM,WinWAP Technologies,Wonu,XDA,Yas,Yospace Ericsson,Yospace Motorola,Yospace Nokia,Yospace Sharp,Yospace Siemens,Yospace SonyEricsson,Yulong,Zonda,ZT,ZTE,.mobi
根据以下 JavaScript 答案,现在更好的解决方案可能是使用如下内容,这可以避免更改用户代理时出现问题。
https://stackoverflow.com/a/24600597/1427373
适用于 ColdFusion:
Per the following JavaScript answer, a better solution may now be to use something like the following, which avoids issues with changing user agents.
https://stackoverflow.com/a/24600597/1427373
Adapted for ColdFusion: