Getting windows username in javascript

sometimes you want to get windows username in your application which is running on intranet. To get the windows username of currently login user i am using javascript.

Here's the code to get windows username in javascript.


<script language="javascript" type="text/javascript">

var WshNetwork = new ActiveXObject("WScript.Network");

alert (WshNetwork.UserName);

</script>

This code snippet will work only in Internet Explorer because Internet Explorer deals with ActiveX Controls. ActiveX is famous for bad security that's why web browsers such as Mozilla Firefox, Google Chrome doesn;t support this.

To get the windows username through just JavaScript is also a big security risk. Therefore it uses ActiveX to get windows username of currently login user.

0 comments: