js获取手机信息

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head lang="en">  
  4.     <meta charset="UTF-8">  
  5.     <title></title>  
  6.     <script src="js/jquery-2.1.4.min.js"></script>  
  7.     <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script>  
  8.     <script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>  
  9. </head>  
  10. <body>  
  11. <script language="javascript" type="text/javascript" src="http://pv.sohu.com/cityjson?ie=utf-8">  
  12. </script>  
  13. <script>  
  14. var map;  
  15. var gpsPoint;  
  16. var baiduPoint;  
  17. var gpsAddress;  
  18. var baiduAddress;  
  19.   
  20.   
  21. //ip ip地址 字符串  
  22. //time 时间 单位秒  
  23. //os 操作系统版本  
  24. //platform 平台 android or ios  
  25. //city 城市  
  26. //location = {lng:XX,lnt:XX} 格式  
  27. //brand 手机品牌  
  28. /* var ip="192.168.1.24";//测试数据  
  29.  var time="2015/12/15 下午13:11";  
  30.  var os="android 4.4.3";  
  31.  var platform="android";  
  32.  var city="成都";  
  33.  var brand="DOA";  
  34.  var lng="250";  
  35.  var lnt="140";*/  
  36. var ip;  
  37. var time;  
  38. var os;  
  39. var platform;  
  40. var city;  
  41. var brand;  
  42. var lng;  
  43. var lnt;  
  44.   
  45. function getLocation() {  
  46.   
  47.     String.prototype.trim = function () {  
  48.   
  49.         return this.replace(/(^s*)|(s*$)/g, '');  
  50.     }  
  51.   
  52.     var test1 = navigator.userAgent.toLocaleString().split("(");  
  53.     var test2 = test1[1].split(")")[0].split(";");  
  54.     if (test2[0] == "iPhone") {  
  55.         platform = "ios";  
  56.         brand = test2[0].trim();  
  57.         os = test2[1].split(" ")[3] + " " + test2[1].split(" ")[4].trim();  
  58.     } else {  
  59.         if (test1.length > 2) {  
  60.             var test2 = test1[1].split(")")[0].split(";");  
  61.             if (test2.length == 5) {  
  62.                 os = test2[2].trim();  
  63.                 //alert(test2[4].split(" ").length)  
  64.                 platform = test2[2].split(" ")[1];  
  65.                 //if(test2[4].split(" ")[0]==" "){  
  66.   
  67.                 //test2[4].split(" ")[1];  
  68.                 //}  
  69.                 if (test2[4].split(" ").length == 4) {  
  70.                     brand = (test2[4].split(" ")[1] + " " + test2[4].split(" ")[2]).trim();  
  71.                 } else {  
  72.                     brand = (test2[4].split(" ")[0] + " " + test2[4].split(" ")[1]).trim();  
  73.                 }  
  74.             }  
  75.         }  
  76.     }  
  77.   
  78.   
  79.     //时间格式化的方法  
  80.     /* Date.prototype.Format = function (fmt) { //author: meizz  
  81.      var o = {  
  82.      "M+": this.getMonth() + 1, //月份  
  83.      "d+": this.getDate(), //日  
  84.      "h+": this.getHours(), //小时  
  85.      "m+": this.getMinutes(), //分  
  86.      "s+": this.getSeconds(), //秒  
  87.      "q+": Math.floor((this.getMonth() + 3) / 3), //季度  
  88.      "S": this.getMilliseconds() //毫秒  
  89.      };  
  90.      if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
  91.      for (var k in o)  
  92.      if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));  
  93.      return fmt;  
  94.      }  
  95.      time =  new Date().Format("yyyy-MM-dd hh:mm");*/  
  96.   
  97.     var d = new Date();  
  98.     var sumS = d.getTime();  
  99.     time = sumS;  
  100.   
  101.     //根据IP获取城市  
  102.     var myCity = new BMap.LocalCity();  
  103.     myCity.get(getCityByIP);  
  104.   
  105.     /* alert("当前时间为" + myDate.toLocaleString());  
  106.      alert("操作系统版本" + navigator.platform)  
  107.      alert("设备其他信息:" + navigator.userAgent);*/  
  108. }  
  109.   
  110. function showMap(value) {  
  111.     var longitude = value.coords.longitude;  
  112.     var latitude = value.coords.latitude;  
  113.     map = new BMap.Map("map");  
  114.     lng = longitude;  
  115.     lnt = latitude;  
  116.     gpsPoint = new BMap.Point(longitude, latitude);    // 创建点坐标  
  117.     map.centerAndZoom(gpsPoint, 15);  
  118.   
  119.     //根据坐标逆解析地址  
  120.     var geoc = new BMap.Geocoder();  
  121.     geoc.getLocation(gpsPoint, getCityByCoordinate);  
  122.   
  123.     BMap.Convertor.translate(gpsPoint, 0, translateCallback);  
  124.   
  125.     /*  alert('ip' + ip);  
  126.      alert("时间" + time);  
  127.      alert('城市' + city);  
  128.      alert('操作系统' + os);  
  129.      alert('平台' + platform);  
  130.      alert("平台长度"+platform.length);  
  131.      alert('设备' + brand);  
  132.      alert('经度' + lnt)  
  133.      alert('纬度' + lng)*/  
  134.     $.ajax({  
  135.         type: "get",  
  136.         async: false,  
  137.         url: "http://192.168.1.34:27017" +  
  138.                 "?ip="  
  139.                 + encodeURIComponent(ip)  
  140.                 + "&time=" + encodeURIComponent(time)  
  141.                 + "&os=" + encodeURIComponent(os)  
  142.                 + "&platform=" + encodeURIComponent(platform)  
  143.                 + "&city=" + encodeURIComponent(city)  
  144.                 + "&brand=" + encodeURIComponent(brand)  
  145.                 + "&lng=" + encodeURIComponent(lng)  
  146.                 + "&lnt=" + encodeURIComponent(lnt),  
  147.         dataType: "jsonp",  
  148.         jsonpCallback: "jsonpCallback",  
  149.         scriptCharset: 'UTF-8',  
  150.   
  151.         success: function (data) {  
  152.             alert("测试成功")  
  153.             window.close();  
  154.             console.log(data);  
  155.         },  
  156.         error: function () {  
  157.             alert("测试成功")  
  158.             window.opener = null;  
  159.             window.open('', '_self');  
  160.             window.close();  
  161.             console.log('fail');  
  162.         }  
  163.   
  164.     });  
  165.     function jsonpCallback(data) {  
  166.         alert("测试成功")  
  167.         window.close();  
  168.         console.log(data)  
  169.     }  
  170.   
  171. }  
  172.   
  173. translateCallback = function (point) {  
  174.     baiduPoint = point;  
  175.     var geoc = new BMap.Geocoder();  
  176.     geoc.getLocation(baiduPoint, getCityByBaiduCoordinate);  
  177. }  
  178.   
  179. function getCityByCoordinate(rs) {  
  180.     gpsAddress = rs.addressComponents;  
  181.     var address = "GPS标注:" + gpsAddress.province + "," + gpsAddress.city + "," + gpsAddress.district + "," + gpsAddress.street + "," + gpsAddress.streetNumber;  
  182.     var marker = new BMap.Marker(gpsPoint);  // 创建标注  
  183.     /* map.addOverlay(marker);              // 将标注添加到地图中*/  
  184.     var labelgps = new BMap.Label(address, { offset: new BMap.Size(20, -10) });  
  185.     /* marker.setLabel(labelgps); //添加GPS标注*/  
  186. }  
  187.   
  188. function getCityByBaiduCoordinate(rs) {  
  189.     baiduAddress = rs.addressComponents;  
  190.     var address = "百度标注:" + baiduAddress.province + "," + baiduAddress.city + "," + baiduAddress.district + "," + baiduAddress.street + "," + baiduAddress.streetNumber;  
  191.     /* alert("当前位置为" + address + "附近");*/  
  192.     var marker = new BMap.Marker(baiduPoint);  // 创建标注  
  193.     map.addOverlay(marker);              // 将标注添加到地图中  
  194.     var labelbaidu = new BMap.Label(address, { offset: new BMap.Size(20, -10) });  
  195.     marker.setLabel(labelbaidu); //添加百度标注  
  196. }  
  197.   
  198. //根据IP获取城市  
  199. function getCityByIP(rs) {  
  200.     var cityName = rs.name;  
  201.     ip = returnCitySN.cip;  
  202.     city = cityName;  
  203.   
  204.     //获取GPS坐标  
  205.     if (navigator.geolocation) {  
  206.         navigator.geolocation.getCurrentPosition(showMap, handleError, { enableHighAccuracy: true, maximumAge: 1000 });  
  207.     } else {  
  208.         alert("您的浏览器不支持使用HTML 5来获取地理位置服务");  
  209.         window.close();  
  210.     }  
  211.     var options = {  
  212.         enableHighAccuracy: true,  
  213.         maximumAge: 10  
  214.     }  
  215.   
  216. }  
  217.   
  218. function handleError(value) {  
  219.     switch (value.code) {  
  220.         case 1:  
  221.             alert("位置服务被拒绝");  
  222.             break;  
  223.         case 2:  
  224.             alert("暂时获取不到位置信息");  
  225.             break;  
  226.         case 3:  
  227.             alert("获取信息超时");  
  228.             break;  
  229.         case 4:  
  230.             alert("未知错误");  
  231.             break;  
  232.     }  
  233.     window.close();  
  234. }  
  235.   
  236. function init() {  
  237.     getLocation();  
  238.   
  239. }  
  240.   
  241. window.onload = init;  
  242.   
  243.   
  244. </script>  
  245.   
  246. <div id="map"></div>  
  247. </body>  
  248. </html>  
原文地址:https://www.cnblogs.com/0828-li/p/7614640.html