วันพฤหัสบดีที่ 24 พฤศจิกายน พ.ศ. 2554

detect chrome browser

โค้ดข้างล่าง ไว้สำหรับ  detect chrome browser แยกขาด safari

$(document).ready(
unction(){
 if ($.browser.mozilla && $.browser.version >= "2.0" ){
  alert('Mozilla above 1.9');
 }

 if( $.browser.safari ){
  var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );
               if( is_chrome  )
  {
alert('Chrome');
  }
  else
  {
  alert('Safari');
  }
 }

 if( $.browser.opera){
  alert('Opera');
 }

 if ($.browser.msie && $.browser.version <= 6 ){
  alert('IE 6 or below version');
 }

 if ($.browser.msie && $.browser.version > 6){
  alert('IE above 6');
 }
                         }  




-ข้างล่างคือ ความรู้ื
On my Windows XP, with Chrome and Safari installed, I noticed that Google Chrome gives me this user agent:


"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.1.249.1064 Safari/532.5"





This means that if you try to detect Safari only, you'll have to check that it doesn't contain "Chrome" :





// A quick solution without using regexp (to speed up a little).


var userAgent = navigator.userAgent.toString().toLowerCase();


if ((userAgent.indexOf('safari') != -1) && !(userAgent.indexOf('chrome') != -1)) {


alert('We should be on Safari only!');


}


เพิ่มเติม

JavaScript to Detect Google Chrome

4
Posted on : 09/05/2008 | By : Jimmy Vu | In : jQuerySolution
The negative side of having a new (and promisingly become popular) browser, no matter how good it can be, is you’ll have to test your web apps with it among many others.
Probably, the first step is to detect the browser from JavaScript code by parsing browser’s user agent, and here is what of Google Chrome.
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US)
AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13
I guess old JavaScript codes can mistakenly tell it Safari like when running the code snippet below using JQuery’s browser utility.
1
2
3
4
$.each($.browser, function(i, val) {
  $("<div>" + i + " : <span>" + val + "</span></div>")
  .appendTo(document.body);
});
It may be no problem until you find something wrong when your apps running on Chrome only. So, here is the code line to detect Chrome generally:
1
var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );
We’ll have to change the JQuery browser utility to support Chrome detection as follows:
1
2
3
4
5
6
7
8
9
10
11
var userAgent = navigator.userAgent.toLowerCase();
 
// Figure out what browser is being used
jQuery.browser = {
 version: (userAgent.match( /.+(?:rv|it|ra|ie|me)[\/: ]([\d.]+)/ ) || [])[1],
 chrome: /chrome/.test( userAgent ),
 safari: /webkit/.test( userAgent ) && !/chrome/.test( userAgent ),
 opera: /opera/.test( userAgent ),
 msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
 mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
};
Now, correct result shows on test screen:
Just one notice: Current version of JQuery (1.2.6) is treating Chrome as if it was Safari and basically no serious problem has been found yet. Modifying browser detection can cause the lib render pages/elements incorrectly for it has no knowledge of Chrome’s rendering engine. To keep compatibility, you can change line 7 back to:
7
safari: /webkit/.test( userAgent ),
But in this case the browser will be detected as both Chrome and Safari — not nice solution, I accept.
Anyway, it’s very likely that JQuery and other JavaScript frameworks will include Chrome to browser list for detection in next versions.

ไม่มีความคิดเห็น:

แสดงความคิดเห็น