admin管理员组

文章数量:1401167

I was experimenting with using a .htm file as a desktop background and i wanted to make it so it would say something like "Wele, 'username'!" where 'username' would be my username that i use to log on to my puter. I have a windows xp puter if that makes a difference. I do not have much experience with javascript, but with some searching i think i found something to use to get the username:

<script language="javascript">
function GetUserName()
{
    var wshell = new ActiveXObject("WScript.Shell");
    alert(wshell.ExpandEnvironmentStrings("%USERNAME%"));
}
</script>

I just found that on the web, i am not sure which part is the acquired username... What i need is to use this later in my html to display on my wallpaper: "Wele, 'username'!" I am not sure if it is needed, but i will include it anyways, the html code i want the wele statement would be in

<center><div class="widget"><div class="title"> *wele statement here* </div></div></center>

Alright thanks guys!!

I was experimenting with using a .htm file as a desktop background and i wanted to make it so it would say something like "Wele, 'username'!" where 'username' would be my username that i use to log on to my puter. I have a windows xp puter if that makes a difference. I do not have much experience with javascript, but with some searching i think i found something to use to get the username:

<script language="javascript">
function GetUserName()
{
    var wshell = new ActiveXObject("WScript.Shell");
    alert(wshell.ExpandEnvironmentStrings("%USERNAME%"));
}
</script>

I just found that on the web, i am not sure which part is the acquired username... What i need is to use this later in my html to display on my wallpaper: "Wele, 'username'!" I am not sure if it is needed, but i will include it anyways, the html code i want the wele statement would be in

<center><div class="widget"><div class="title"> *wele statement here* </div></div></center>

Alright thanks guys!!

Share Improve this question asked Dec 25, 2013 at 0:54 user3133613user3133613 111 gold badge1 silver badge3 bronze badges 2
  • Does the alert work? If so, select your title element (document.querySelector(".widget .title"), maybe? I’d give it an ID – not sure which browser engine Active Desktop uses) and put the returned value in its innerHTML. (Or, better, create a text node and append it…) – Ry- Commented Dec 25, 2013 at 0:58
  • 1 fyi that won't work in non-ie browsers. – Daniel A. White Commented Dec 25, 2013 at 1:07
Add a ment  | 

1 Answer 1

Reset to default 2

Give your title DIV an ID:

<center><div class="widget"><div class="title" id="wele"> Wele </div></div></center>

Then use the following Javascript:

window.addEventListener('load', function() {
    var username = GetUserName();
    document.getElementById('wele').innerHTML = 'Wele, '+username;
});

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

本文标签: javascriptUse windows username in htmlStack Overflow