1 <!DOCTYPE html>
2 <html>
3 <head lang="en">
4 <meta charset="UTF-8">
5 <title></title>
6 </head>
7 <body>
8
9 <script>
10 // get the maximum downlink speed
11 // var speed = downlinkmax();
12 //
13 // // check if it is enough for our amazing feature
14 // if (speed < sufficient) {
15 // disableFeature();
16 // }
17
18 // (function (window, factory) {
19 // 'use strict';
20 // if (typeof define === 'function' && define.amd) {
21 // // AMD
22 // define([], factory);
23 // } else if (typeof exports === 'object') {
24 // // Node.js
25 // module.exports = factory();
26 // } else {
27 // // Browser
28 // window.downlinkmax = factory();
29 // }
30 // }(this, function factory() {
31 // // public API
32 // return $1;
33 // }));
34
35 // 方法一
36 var limitless = Infinity,
37 nav = navigator,
38 speed,
39
40 connection = nav.connection || nav.mozConnection || nav.webkitConnection || {
41 // 不支持API
42 downlinkMax: limitless
43 };
44
45 switch (connection.type) {
46 case 'none':
47 speed = 0;
48 break;
49 case '2g':
50 speed = 0.134;
51 break;
52 case 'bluetooth':
53 case 'cellular':
54 speed = 2;
55 break;
56 case '3g':
57 speed = 8.95;
58 break;
59 case '4g':
60 speed = 100;
61 break;
62 case 'ethernet':
63 speed = 550;
64 break;
65 case 'wifi':
66 speed = 600;
67 break;
68 // 其他、未知等.
69 default:
70 speed = limitless;
71 break;
72 }
73 connection.downlinkMax = speed;
74 console.log(connection);
75
76
77
78
79 // var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
80 // var type = connection.type;
81 // function updateConnectionStatus() {
82 // alert("Connection type is change from " + type + " to " + connection.type);
83 // }
84 // connection.addEventListener('typechange', updateConnectionStatus);
85
86
87
88 // 方法二
89 var connection = navigator.connection||navigator.mozConnection||navigator.webkitConnection||{tyep:'unknown'};
90 var type_text = ['unknown','ethernet','wifi','2g','3g','4g','none'];
91
92 alert(connection.type);
93 var re_el = document.getElementById("re");
94 var btn_el = document.getElementById("btn");
95 function get_status(){
96 if(typeof(connection.type) == "number"){
97 connection.type_text = type_text[connection.type];
98 }else{
99 connection.type_text = connection.type;
100 }
101 if(typeof(connection.bandwidth) == "number"){
102 if(connection.bandwidth > 10){
103 connection.type = 'wifi';
104 }else if(connection.bandwidth > 2){
105 connection.type = '3g';
106 }else if(connection.bandwidth > 0){
107 connection.type = '2g';
108 }else if(connection.bandwidth == 0){
109 connection.type = 'none';
110 }else{
111 connection.type = 'unknown';
112 }
113 }
114 var html = 'Type : '+connection.type_text;
115 html += '<br>Bandwidth : '+connection.bandwidth;
116 html += '<br>isOnline : '+navigator.onLine;
117 re_el.innerHTML = html;
118 }
119
120 btn_el.onclick = function(){
121 re_el.innerHTML = 'Waiting...';
122 get_status();
123 }
124
125
126
127
128 // get the maximum downlink speed
129 // speed = downlinkmax()
130
131 // check if it is enough for our amazing feature
132 // disableFeature() if speed < sufficient
133 </script>
134
135 </body>
136 </html>