CSS

Wednesday, February 8, 2012

JavaScript: Browser Name and Version

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOnElHkfZKB-VhR28yGQj-SNy45EKgFtn59me8grqopWhEkseB1g4q45rP1gb2aB_7gO356DH0ZXPL4UiYtxacEFuDPGWNnbJJgkOcETSC7117zew3vo4TPC-nvgHQqDKb5il5rCYYRQc/s1600/grey_plaid.jpg https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgasRBFjrNiQZJ-Rs-gp99P-Brq5elXXve6XrDvM006PL8vlXNVEGQD-GqVGt4-jGSLlX0WC3FeLaJ7LDbz31rKX6rIGBioWGgNH8gLuW5bgg1sROpkMbBJM1Enl7e80SjvQmacOrdWuo/s1600/grey_plaid.png

Browser Detection

To establish the actual name of the user's Web browser, we can use the navigator.appName and navigator.userAgent properties.
Firefox (and some other browsers) may return the string Netscape as the value of navigator.appName for compatibility with Netscape Navigator.
However, that navigator.userAgent may be spoofed, too – that is, clients may substitute virtually any string for their userAgent.


Script Hasil Modif

function detectBrowser(){
  var nVer=navigator.appVersion;
  var nAgt=navigator.userAgent;
  var browserName =navigator.appName;
  var fullVersion =parseFloat(navigator.appVersion); 
  var majorVersion=parseInt(navigator.appVersion,10);
  var nameOffset,verOffset,ix;

  if((verOffset=nAgt.indexOf("Opera"))!=-1){
    browserName="Opera"; 
    fullVersion=nAgt.substring(verOffset+6);
    if((verOffset=nAgt.indexOf("Version"))!=-1){fullVersion=nAgt.substring(verOffset+8)}
  }else if((verOffset=nAgt.indexOf("MSIE"))!=-1){
    browserName="Microsoft Internet Explorer"; 
    fullVersion=nAgt.substring(verOffset+5);
  }else if((verOffset=nAgt.indexOf("Chrome"))!=-1){
    browserName="Chrome"; 
    fullVersion=nAgt.substring(verOffset+7);
  }else if((verOffset=nAgt.indexOf("Safari"))!=-1){
    browserName="Safari"; 
    fullVersion=nAgt.substring(verOffset+7);
    if((verOffset=nAgt.indexOf("Version"))!=-1){fullVersion=nAgt.substring(verOffset+8)}
  }else if((verOffset=nAgt.indexOf("Firefox"))!=-1){
    browserName="Firefox"; 
    fullVersion=nAgt.substring(verOffset+8);
  }else if( (nameOffset=nAgt.lastIndexOf(' ')+1) < (verOffset=nAgt.lastIndexOf('/')) ){
    browserName=nAgt.substring(nameOffset,verOffset); 
    fullVersion=nAgt.substring(verOffset+1);
    if(browserName.toLowerCase()==browserName.toUpperCase()){browserName=navigator.appName}
  }
  if((ix=fullVersion.indexOf(";"))!=-1){fullVersion=fullVersion.substring(0,ix)}
  if((ix=fullVersion.indexOf(" "))!=-1){fullVersion=fullVersion.substring(0,ix)}
  majorVersion=parseInt(fullVersion,10);
  if(isNaN(majorVersion)){
    fullVersion=parseFloat(navigator.appVersion); 
    majorVersion=parseInt(navigator.appVersion,10);
  }
  var tulis=browserName+' '+majorVersion;
  return tulis;
}

document.write(detectBrowser());