第一种, Javascript
[javascript] view plaincopyprint?
-
[javascript] view plaincopyprint?在CODE上查看代码片派生到我的代码片 var deviceAgent = navigator.userAgent.toLowerCase(); var agentID = deviceAgent.match(/(iphone|ipod|ipad|android)/); if(agentID.indexOf("iphone")>=0){ alert("iphone"); } if(agentID.indexOf("ipod")>=0){ alert("ipod"); } if(agentID.indexOf("ipad")>=0){ alert("ipad"); } if(agentID.indexOf("android")>=0){ alert("android"); }
<!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <style type="text/css"> html { height: 100% } body { height: 100%; margin: 0px; padding: 0px } </style> <script type="text/javascript"> function initialize() { var useragent = navigator.userAgent; if (useragent.indexOf(‘iPhone‘) != -1) { alert("iPhone"); } else if(useragent.indexOf(‘iPad‘) != -1 ) { alert("iPad"); } else if(useragent.indexOf(‘Android‘) != -1 ) { alert("Android"); } else { alert("其他平台"); } } </script> </head> <body onload="initialize()"> </body> </html>
第二种 Java:
[java] view plaincopyprint?
-
Enumeration typestr = request.getHeaderNames(); String s1 = request.getHeader("user-agent"); if(s1.contains("Android")) { System.out.println("Android移动客户端"); } else if(s1.contains("iPhone")) { System.out.println("iPhone移动客户端"); } else if(s1.contains("iPad")) { System.out.println("iPad客户端"); } else { System.out.println("其他客户端"); }
整个页面 index.jsp:
-
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <link rel="shortcut icon" href="favicon.ico"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% Enumeration typestr = request.getHeaderNames(); String s1 = request.getHeader("user-agent"); if(s1.contains("Android")) { System.out.println("Android移动客户端"); } else if(s1.contains("iPhone")) { System.out.println("iPhone移动客户端"); } else if(s1.contains("iPad")) { System.out.println("iPad客户端"); } else { System.out.println("其他客户端"); } %> </body> </html>
这两种方法测试下来,一般情况下可以满足需要,遇到像UC浏览器这些第三方浏览器,可能就不准了!
原文出自:http://blog.csdn.net/feng88724/article/details/7312827
时间: 2024-11-02 02:18:08