How to Use ActiveXObject to Get Username in JavaScript
-
ActiveXObject
in JavaScript -
Use
ActiveXObject('wscript.Network')
to Get Username in JavaScript -
Use
ActiveXObject('wscript.shell')
to Get Username in JavaScript
For retrieving information about the current system username, we have to ping it to the server. We can get the browser’s data and the Operating systems detail via procedures like the navigator
object and its properties.
But most accepted ways of performing this task have been disabled for most browsers. Here, we will mention one method to get the system’s username that works in Internet Explorer.
A constraint is the functionality of the ActiveXObject
object might expire after June 2022, but we will still use this object in the following examples. Another way is to use SharePoint
and sp.js
to get the username.
For this method, you can follow up on this article.
ActiveXObject
in JavaScript
This segment will see two ways of working with the ActiveXObject
object. The first method is the wscript.Network
, and the second is wscript.shell
.
Use ActiveXObject('wscript.Network')
to Get Username in JavaScript
The WScript.Network
enables access to a shared resource of the network. So, the PC where we are performing the ping job will reflect the username and other necessary detail through the shared network protocol.
Code Snippet:
<!doctype html>
<html>
<head>
<title>Windows Username</title>
</head>
<body>
<script type="text/javascript">
var Network = new ActiveXObject("wscript.Network");
alert(Network.UserName);
</script>
</body>
</html>
Output:
Use ActiveXObject('wscript.shell')
to Get Username in JavaScript
In the case of WScript.shell
, you can easily display detail on the user, run applications, or may control windows environment variables. From triggering the shell, we will return the user’s name.
Code Snippet:
<!doctype html>
<html>
<head>
<title>Windows Username</title>
</head>
<body>
<script type="text/javascript">
var wshell=new ActiveXObject("wscript.shell");
var username=wshell.ExpandEnvironmentStrings("%username%");
alert(username);
</script>
</body>
</html>
Output: