4

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.

Lakshmi
  • 175
  • 1
  • 4
  • 16
  • 6
    Uhm. I'm surprised you can do this in IE! This is incredibly intrusive and shouldn't be done. I'm pretty sure that _privacy conscious_ browsers like Chrome and Safari dont do that. They also dont support ActiveX. Thank god. – somethinghere Mar 24 '15 at 12:06

2 Answers2

4

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?

Community
  • 1
  • 1
Om.
  • 2,532
  • 4
  • 22
  • 22
  • The javascript for firefox is not working properly.. And am looking mainly to get the user name for the following three browsers -IE -Firefox -Chrome Is there any other way to find in all three browswers? – Bhuvanesh Waran Nov 17 '16 at 03:40
  • 1
    what about chrome? – Idan Yehuda Jun 22 '17 at 10:29
  • 1
    The JavaScript you have posted for Firefox is only valid in a legacy extension. It isn't valid in a normal webpage. Including it here is inappropriate. – Makyen Oct 07 '17 at 06:44
3

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.

Eric
  • 18,532
  • 2
  • 34
  • 39
  • 1
    But, just to add, even using an applet shouldn't really be done. If you have to ask for user permission anyway, just ask the user to type in their name in a dialog or something. Otherwise this privacy disaster should be avoided. – somethinghere Mar 24 '15 at 12:12