Wednesday, March 15, 2017

Get user browser name | get user ip address

It is always a good thing to collect browser info because depend on that developer can make application more usable for the user. And also we can make our application more compatible with the browser which user is using mostly.

Get browser name

Standard way is to collect user agent of the browser, see below code to get user agent info.


var browser = window.navigator.userAgent;

console.log(browser);

Eg result: 
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 
(KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36


Above one is the most suggested way, but if you want to collect the name of the browser you can use below code.


 $(document).ready(function () {

 var browserName = 'Unknown';
 
 var isOpera = (!!window.opr && !!opr.addons) || !!window.opera 
 || navigator.userAgent.indexOf(' OPR/') >= 0;
 
 var isIE = /*@cc_on!@*/false || !!document.documentMode;
 
 var isChrome = !!window.chrome && !!window.chrome.webstore;
 
 if((!!window.opr && !!opr.addons) || !!window.opera 
 || navigator.userAgent.indexOf(' OPR/') >= 0){
  browserName = 'Opera';
 }
 if(typeof InstallTrigger !== 'undefined'){
  browserName = 'FirFox';
 }
 if(Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0){
  browserName = 'Safari';
 }
 if(/*@cc_on!@*/false || !!document.documentMode){
  browserName = 'Internet Explorer';
 }
 if(!isIE && !!window.StyleMedia){
  browserName = 'Microsoft Edge';
 }
 if(!!window.chrome && !!window.chrome.webstore){
  browserName = 'Chrome';
 }
 
 console.log(browserName);

})


Get IP of user

For collecting IP address of user, you can use below code.


$(document).ready(function () {
 
 $.getJSON("//jsonip.com/?callback=?", function (data) {
  
  var ipAddress = data.ip;
  
  console.log(ipAddress);
 });

});


Related Post

1. HTTP status codes and meanings.

2. Jquery select all/de select all checkbox

3. How to download file from backend to UI

No comments :

Post a Comment