Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get windows user name using javascript

I tried to get the windows user name in IE browser only using the below code.

function GetUserName() 
{ 
    var wshell = new ActiveXObject("WScript.Shell"); 
    alert(wshell.ExpandEnvironmentStrings("%USERNAME%")); 
} 

How to get the windows username in other browsers like chrome, mozilla, safari.

like image 528
Lakshmi Avatar asked Mar 17 '23 15:03

Lakshmi


2 Answers

I Agree with Eric comment. However try following hacks. Most likely to work in win7

In IE

var objUserInfo = new ActiveXObject("WScript.network");
document.write(objUserInfo.ComputerName+"<br>"); 
document.write(objUserInfo.UserDomain+"<br>"); 
document.write(objUserInfo.UserName+"<br>");  
var uname =  objUserInfo.UserName;
alert(uname);

Firefox:

function getUser() {
   return Components
     .classes["@mozilla.org/process/environment;1"]
     .getService(Components.interfaces.nsIEnvironment)
     .get('USERNAME');
}  

Source: Get OS Win 7 username Javascript

JavaScript - How to get the name of the current user

Get Windows username with JavaScript?

like image 173
Om. Avatar answered Mar 28 '23 13:03

Om.


As of today it is impossible, and will most likely be in the future as well.

The only quirk I can come up with; is to run a custom Java-Applet that checks it for you. However this is a passive action, and thus, you can not automatically extract it without the clients permission.

like image 29
Eric Avatar answered Mar 28 '23 15:03

Eric