js檢測蘋果終端裝置程式碼例項

螞蟻小編發表於2017-03-16

在實際應用中,可能需要檢測iPhone、iPod、iPad等裝置,下面就通過程式碼例項簡單介紹一下如何實現此功能。

程式碼如下:

[JavaScript] 純文字檢視 複製程式碼
var Apple = {};
Apple.UA = navigator.userAgent;
Apple.Device = false;
Apple.Types = ["iPhone", "iPod", "iPad"];
for (var d = 0; d < Apple.Types.length; d++) {
  var t = Apple.Types[d];
  Apple[t] = !!Apple.UA.match(new RegExp(t, "i"));
  Apple.Device = Apple.Device || Apple[t];
}
alert(
  "Apple device? " + Apple.Device +
  "\niPhone? " + Apple.iPhone +
  "\niPod? " + Apple.iPod +
  "\niPad? " + Apple.iPad
);


相關文章