[Mobile Web] How to differentiate mobile devices in the Web? (IPad, IPhone, Android)

Source: Internet
Author: User

First, Javascript

[JavaScript]View Plaincopyprint?
  1. [JavaScript] View plaincopyprint?View code slices to my Code slicevarDeviceAgent =navigator.userAgent.toLowerCase (); varAgentid = 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>  <Metaname= "Viewport"content= "initial-scale=1.0, User-scalable=no" />  <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">  <styletype= "Text/css">HTML{Height:100%}Body{Height:100%;margin:0px;padding:0px}  </style>  <Scripttype= "Text/javascript">    functionInitialize () {varuseragent=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 ("Other Platforms"); }    }  </Script>  </Head>  <Bodyonload= "Initialize ()">  </Body>  </HTML>  

The second type of Java:

[Java]View Plaincopyprint?
  1. Enumeration   Typestr = request.getheadernames ();    = Request.getheader ("user-agent");   if (S1.contains ("Android")) {      System.out.println ("Android mobile client");   Else if (S1.contains ("iphone")) {      System.out.println ("iphone Mobile client");  }   Else if (S1.contains ("ipad")) {      System.out.println ("ipad client");  }   Else {      System.out.println ("Other client");  }  

Entire page index.jsp:

 
  1. <%@ 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" > Enumeration Typestr=Request.getheadernames (); String S1= Request.getheader ("User-agent"); if(S1.contains ("Android") {System.out.println ("Android Mobile Client"); } Else if(S1.contains ("IPhone") {System.out.println ("Iphone Mobile Client"); }  Else if(S1.contains ("IPad") {System.out.println ("ipad Client"); }  Else{System.out.println ("Other Clients"); }      %> </body> 

These two methods test down, in general can meet the needs, encounter like the UC browser these third-party browsers, may not be allowed!

Source: http://blog.csdn.net/feng88724/article/details/7312827

[Mobile Web] How to differentiate mobile devices in the Web? (IPad, IPhone, Android)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.