program tip

JavaScript 또는 jQuery를 사용하여 Mac OS X 또는 Windows 컴퓨터를 감지하는 가장 좋은 방법

radiobox 2020. 8. 28. 07:17
반응형

JavaScript 또는 jQuery를 사용하여 Mac OS X 또는 Windows 컴퓨터를 감지하는 가장 좋은 방법


그래서 저는 "닫기"버튼을 사용자가 Mac에있을 때는 왼쪽으로, 사용자가 PC에있을 때는 오른쪽으로 이동하려고합니다. 이제 사용자 에이전트를 검사하여 수행하고 있지만 신뢰할 수있는 OS 감지를 위해 너무 쉽게 스푸핑 될 수 있습니다. 브라우저가 실행되는 OS가 Mac OS X인지 Windows인지를 감지하는 확실한 방법이 있습니까? 그렇지 않다면 사용자 에이전트 스니핑보다 나은 것은 무엇입니까?


window.navigator.platform의 사용자 에이전트 문자열이 변경 될 때 속성이 스푸핑되지 않습니다. userAgent를 iPhone 또는 Chrome Windows로 변경하면 Mac에서 테스트 했는데 navigator.platform 은 MacIntel로 유지됩니다.

userAgent 문자열이 변경 될 때 navigator.platform이 스푸핑되지 않습니다.

속성도 읽기 전용입니다.

navigator.platform은 읽기 전용입니다.


나는 다음 표를 생각 해낼 수 있었다

Mac 컴퓨터

Mac68K Macintosh 68K 시스템.
MacPPC Macintosh PowerPC 시스템.
MacIntel Macintosh Intel 시스템.

iOS 기기

iPhone 아이폰.
iPod 아이팟 터치.
iPad iPad.


현대의 맥이 돌아 navigator.platform == "MacIntel"왔지만 일부 "미래 증명"을 제공하기 위해 정확한 매칭을 사용하지 않습니다. 바라건대 그들은 비슷 MacARM하거나 MacQuantum미래에 변경 될 것 입니다.

var isMac = navigator.platform.toUpperCase().indexOf('MAC')>=0;

"왼쪽"도 사용하는 iOS를 포함하려면

var isMacLike = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
var isIOS = /(iPhone|iPod|iPad)/i.test(navigator.platform);

var is_OSX = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
var is_iOS = /(iPhone|iPod|iPad)/i.test(navigator.platform);

var is_Mac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
var is_iPhone = navigator.platform == "iPhone";
var is_iPod = navigator.platform == "iPod";
var is_iPad = navigator.platform == "iPad";

/* Output */
var out = document.getElementById('out');
if (!is_OSX) out.innerHTML += "This NOT a Mac or an iOS Device!";
if (is_Mac) out.innerHTML += "This is a Mac Computer!\n";
if (is_iOS) out.innerHTML += "You're using an iOS Device!\n";
if (is_iPhone) out.innerHTML += "This is an iPhone!";
if (is_iPod) out.innerHTML += "This is an iPod Touch!";
if (is_iPad) out.innerHTML += "This is an iPad!";
out.innerHTML += "\nPlatform: " + navigator.platform;
<pre id="out"></pre>


대부분의 OS는 오른쪽에있는 닫기 버튼을 사용하기 때문에 사용자가 MacLike OS를 사용 중일 때 닫기 버튼을 왼쪽으로 이동하면됩니다. 그렇지 않으면 가장 일반적인 쪽인 오른쪽에두면 문제가되지 않습니다.

setTimeout(test, 1000); //delay for demonstration

function test() {

  var mac = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);

  if (mac) {
    document.getElementById('close').classList.add("left");
  }
}
#window {
  position: absolute;
  margin: 1em;
  width: 300px;
  padding: 10px;
  border: 1px solid gray;
  background-color: #DDD;
  text-align: center;
  box-shadow: 0px 1px 3px #000;
}
#close {
  position: absolute;
  top: 0px;
  right: 0px;
  width: 22px;
  height: 22px;
  margin: -12px;
  box-shadow: 0px 1px 3px #000;
  background-color: #000;
  border: 2px solid #FFF;
  border-radius: 22px;
  color: #FFF;
  text-align: center;
  font: 14px"Comic Sans MS", Monaco;
}
#close.left{
  left: 0px;
}
<div id="window">
  <div id="close">x</div>
  <p>Hello!</p>
  <p>If the "close button" change to the left side</p>
  <p>you're on a Mac like system!</p>
</div>

http://www.nczonline.net/blog/2007/12/17/don-t-forget-navigator-platform/


다음과 같이 간단합니다.

function isMacintosh() {
  return navigator.platform.indexOf('Mac') > -1
}

function isWindows() {
  return navigator.platform.indexOf('Win') > -1
}

다음과 같은 재미있는 일을 할 수 있습니다.

var isMac = isMacintosh();
var isPC = !isMacintosh();

이것이 당신이 찾고있는 것입니까? 그렇지 않은 경우 알려 주시면이 게시물을 제거하겠습니다.

Try this jQuery plugin: http://archive.plugins.jquery.com/project/client-detect

Demo: http://www.stoimen.com/jquery.client.plugin/

This is based on quirksmode BrowserDetect a wrap for jQuery browser/os detection plugin.

For keen readers:
http://www.stoimen.com/blog/2009/07/16/jquery-browser-and-os-detection-plugin/
http://www.quirksmode.org/js/support.html

And more code around the plugin resides here: http://www.stoimen.com/jquery.client.plugin/jquery.client.js


Let me know if this works. Way to detect an Apple device (Mac computers, iPhones, etc.) with help from StackOverflow.com:
What is the list of possible values for navigator.platform as of today?

var deviceDetect = navigator.platform;
var appleDevicesArr = ['MacIntel', 'MacPPC', 'Mac68K', 'Macintosh', 'iPhone', 
'iPod', 'iPad', 'iPhone Simulator', 'iPod Simulator', 'iPad Simulator', 'Pike 
v7.6 release 92', 'Pike v7.8 release 517'];

// If on Apple device
if(appleDevicesArr.includes(deviceDetect)) {
    // Execute code
}
// If NOT on Apple device
else {
    // Execute code
}

참고 URL : https://stackoverflow.com/questions/10527983/best-way-to-detect-mac-os-x-or-windows-computers-with-javascript-or-jquery

반응형