Programing

브라우저가 jQuery를 사용하는 Chrome인지 감지하는 방법은 무엇입니까?

lottogame 2020. 12. 28. 07:43
반응형

브라우저가 jQuery를 사용하는 Chrome인지 감지하는 방법은 무엇입니까?


Safari, 두 웹킷 브라우저 모두에서 제대로 작동하는 크롬에서 실행되는 기능에 약간의 문제가 있습니다.

Chrome 용 함수에서 변수를 사용자 정의해야하지만 Safari 용은 아닙니다.

슬프게도 나는 이것을 사용하여 웹킷 브라우저인지 감지했습니다.

if ($.browser.webkit) {

하지만 다음을 감지해야합니다.

if ($.browser.chrome) {

비슷한 문장 (위의 동작 버전)을 작성하는 방법이 있습니까?


$.browser.chrome = /chrom(e|ium)/.test(navigator.userAgent.toLowerCase()); 

if($.browser.chrome){
  ............

}

업데이트 : (@ Mr. Bacciagalupe에게 10x)

jQuery는 1.9 및 최신 릴리스 $.browser에서 제거 되었습니다.

하지만 $ .browser를 여기에 있는 독립 실행 형 플러그인으로 사용할 수 있습니다.


if(/chrom(e|ium)/.test(navigator.userAgent.toLowerCase())){
 alert('I am chrome');
}

이 질문은 이미 여기에서 논의되었습니다 : JavaScript : 사용자 브라우저가 Chrome인지 확인하는 방법?

이것을 시도하십시오 :

var isChromium = window.chrome;
if(isChromium){
    // is Google chrome 
} else {
    // not Google chrome 
}

그러나 더 완전하고 정확한 대답은 IE11, IE 에지 이후이 될 것이며, 오페라는 또한 반환 true에 대한window.chrome

따라서 아래를 사용하십시오.

// please note, 
// that IE11 now returns undefined again for window.chrome
// and new Opera 30 outputs true for window.chrome
// but needs to check if window.opr is not undefined
// and new IE Edge outputs to true now for window.chrome
// and if not iOS Chrome check
// so use the below updated condition
var isChromium = window.chrome;
var winNav = window.navigator;
var vendorName = winNav.vendor;
var isOpera = typeof window.opr !== "undefined";
var isIEedge = winNav.userAgent.indexOf("Edge") > -1;
var isIOSChrome = winNav.userAgent.match("CriOS");

if (isIOSChrome) {
   // is Google Chrome on IOS
} else if(
  isChromium !== null &&
  typeof isChromium !== "undefined" &&
  vendorName === "Google Inc." &&
  isOpera === false &&
  isIEedge === false
) {
   // is Google Chrome
} else { 
   // not Google Chrome 
}

위의 게시물은 jQuery.browser를 사용하는 것이 좋습니다. 그러나 jQuery API는이 방법을 사용하지 말 것을 권장합니다 . .. ( API의 DOCS 참조 ). 그리고 그 기능은 향후 jQuery 릴리스에서 팀 지원 플러그인으로 이동 될 수 있음을 설명합니다.

jQuery API는 jQuery.support.

그 이유는 'jQuery.browser'가 스푸핑 될 수있는 사용자 에이전트를 사용하고 실제로 jQuery의 이후 버전에서 더 이상 사용되지 않기 때문입니다. 정말로 $ .browser를 사용하고 싶다면 .. 여기에 독립형 jQuery 플러그인에 대한 링크가 있습니다. 이것은 jQuery 버전 1.9.1에서 제거 되었기 때문입니다. https://github.com/gabceb/jquery-browser-plugin

It's better to use feature object detection instead of browser detection.

Also if you use the Google Chrome inspector and go to the console tab. Type 'window' and press enter. Then you be able to view the DOM properties for the 'window object'. When you collapse the object you can view all the properties, including the 'chrome' property.

I hope this helps, even though the question was how to do with with jQuery. But sometimes straight javascript is more simple!


User Endless is right,

$.browser.chrome = (typeof window.chrome === "object"); 

code is best to detect Chrome browser using jQuery.

If you using IE and added GoogleFrame as plugin then

var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );

code will treat as Chrome browser because GoogleFrame plugin modifying the navigator property and adding chromeframe inside it.


var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );

userAgent can be changed. for more robust, use the global variable specified by chrome

$.browser.chrome = (typeof window.chrome === "object");

Although it is not Jquery , I use jquery myself but for browser detection I have used the script on this page a few times. It detects all major browsers, and then some. The work is pretty much all done for you.


Sadly due to Opera's latest update !!window.chrome (and other tests on the window object) when testing in Opera returns true.

Conditionizr takes care of this for you and solves the Opera issue:

conditionizr.add('chrome', [], function () {
    return !!window.chrome && !/opera|opr/i.test(navigator.userAgent);
});

I'd highly suggest using it as none of the above are now valid.

This allows you to do:

if (conditionizr.chrome) {...}

Conditionizr takes care of other browser detects and is much faster and reliable than jQuery hacks.


As a quick addition, and I'm surprised nobody has thought of this, you could use the in operator:

"chrome" in window

Obviously this isn't using JQuery, but I figured I'd put it since it's handy for times when you aren't using any external libraries.


if(navigator.vendor.indexOf('Goog') > -1){
  //Your code here
}

When I test the answer @IE, I got always "true". The better way is this which works also @IE:

var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);

As described in this answer: https://stackoverflow.com/a/4565120/1201725

ReferenceURL : https://stackoverflow.com/questions/6339480/how-to-detect-if-a-browser-is-chrome-using-jquery

반응형