最近一直在做兼容移动终端的网站,下面是总结的一个函数,希望在做移动网站开发的的朋友们能有一个参考:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
 
<span style="color: #003366;font-weight: bold">function</span> checkMobile<span style="color: #009900">(</span><span style="color: #009900">)</span>
<span style="color: #009900">{</span>
	<span style="color: #003366;font-weight: bold">var</span> pda_user_agent_list <span style="color: #339933">=</span> <span style="color: #003366;font-weight: bold">new</span> Array<span style="color: #009900">(</span><span style="color: #3366CC">"2.0 MMP"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"240320"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"AvantGo"</span><span style="color: #339933">,</span><span style="color: #3366CC">"BlackBerry"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"Blazer"</span><span style="color: #339933">,</span>
			<span style="color: #3366CC">"Cellphone"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"Danger"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"DoCoMo"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"Elaine/3.0"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"EudoraWeb"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"hiptop"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"IEMobile"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"KYOCERA/WX310K"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"LG/U990"</span><span style="color: #339933">,</span>
			<span style="color: #3366CC">"MIDP-2.0"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"MMEF20"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"MOT-V"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"NetFront"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"Newt"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"Nintendo Wii"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"Nitro"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"Nokia"</span><span style="color: #339933">,</span>
			<span style="color: #3366CC">"Opera Mini"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"Opera Mobi"</span><span style="color: #339933">,</span>
			<span style="color: #3366CC">"Palm"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"Playstation Portable"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"portalmmm"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"Proxinet"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"ProxiNet"</span><span style="color: #339933">,</span>
			<span style="color: #3366CC">"SHARP-TQ-GX10"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"Small"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"SonyEricsson"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"Symbian OS"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"SymbianOS"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"TS21i-10"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"UP.Browser"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"UP.Link"</span><span style="color: #339933">,</span>
			<span style="color: #3366CC">"Windows CE"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"WinWAP"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"Android"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"iPhone"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"iPod"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"iPad"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"Windows Phone"</span><span style="color: #339933">,</span> <span style="color: #3366CC">"HTC"</span><span style="color: #006600;font-style: italic">/*, "GTB"*/</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #003366;font-weight: bold">var</span> pda_app_name_list <span style="color: #339933">=</span> <span style="color: #003366;font-weight: bold">new</span> Array<span style="color: #009900">(</span><span style="color: #3366CC">"Microsoft Pocket Internet Explorer"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #006600;font-style: italic">//转载注明www.chhua.com</span>
	<span style="color: #003366;font-weight: bold">var</span> user_agent <span style="color: #339933">=</span> navigator.<span style="color: #660066">userAgent</span>.<span style="color: #660066">toString</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #000066;font-weight: bold">for</span> <span style="color: #009900">(</span><span style="color: #003366;font-weight: bold">var</span> i<span style="color: #339933">=</span><span style="color: #CC0000">0</span><span style="color: #339933">;</span> i<span style="color: #339933"><</span>pda_user_agent_list.<span style="color: #660066">length</span><span style="color: #339933">;</span> i<span style="color: #339933">++</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
		<span style="color: #000066;font-weight: bold">if</span> <span style="color: #009900">(</span>user_agent.<span style="color: #660066">indexOf</span><span style="color: #009900">(</span>pda_user_agent_list<span style="color: #009900">[</span>i<span style="color: #009900">]</span><span style="color: #009900">)</span> <span style="color: #339933">>=</span> <span style="color: #CC0000">0</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
			<span style="color: #000066;font-weight: bold">return</span> <span style="color: #003366;font-weight: bold">true</span><span style="color: #339933">;</span>
		<span style="color: #009900">}</span>
	<span style="color: #009900">}</span>
	<span style="color: #003366;font-weight: bold">var</span> appName <span style="color: #339933">=</span> navigator.<span style="color: #660066">appName</span>.<span style="color: #660066">toString</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #000066;font-weight: bold">for</span> <span style="color: #009900">(</span><span style="color: #003366;font-weight: bold">var</span> i<span style="color: #339933">=</span><span style="color: #CC0000">0</span><span style="color: #339933">;</span> i<span style="color: #339933"><</span>pda_app_name_list.<span style="color: #660066">length</span><span style="color: #339933">;</span> i<span style="color: #339933">++</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
		<span style="color: #000066;font-weight: bold">if</span> <span style="color: #009900">(</span>user_agent.<span style="color: #660066">indexOf</span><span style="color: #009900">(</span>pda_app_name_list<span style="color: #009900">[</span>i<span style="color: #009900">]</span><span style="color: #009900">)</span> <span style="color: #339933">>=</span> <span style="color: #CC0000">0</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
			<span style="color: #000066;font-weight: bold">return</span> <span style="color: #003366;font-weight: bold">true</span><span style="color: #339933">;</span>
		<span style="color: #009900">}</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000066;font-weight: bold">return</span> <span style="color: #003366;font-weight: bold">false</span><span style="color: #339933">;</span>
<span style="color: #009900">}</span>