這個隨筆純粹是小知識的積累,以后都會打上小知識的標簽。
經常見的,下載移動app時,只有一個二維碼,但掃碼后,會根據手機是iphone還是android下載不同app,下面就是這個操作的代碼:
< !DOCTYPE html > <html > <head > </head> <body> <script> var ua = navigator.userAgent.toLowerCase(); if (/iphone | ipad | ipod / .test(ua)) { alert("iphone"); } else if (/android/.test(ua)) { alert("android"); } else { alert('unknow'); } < /script> </body > </html>
這里主要是通過navigator.userAgent 獲取標識,然后會輸出下面類似的一個字符串,然后根據關鍵字判斷標識。很簡單吧。
"mozilla/5.0 (iphone; cpu iphone os 8_0 like mac os x) applewebkit/600.1.3 (khtml, like gecko) version/8.0 mobile/12a4345d safari/600.1.4"
轉載請注明出處:http://zhutty.cnblogs.com