Here is the method I use for querying LDAP for all sorts of useful info, such as username. Written in VBScript (sorry I'm not proficient at Javascript).
Dim txtLogon
'set the variables
Set objRootDSE = GetObject("LDAP://rootDSE")
Set objSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
'Use this when your users logon via a smartcard or by means
'other than username and password.
txtLogon = objUser.Get("sAMAccountName") 'Logon Name (pre-Windows 2000)
'Use this when your users logon via username and password. (most common)
txtLogon = objUser.Get("userPrincipalName")
Bonus: Here is a great reference guide to Active Directory.
http://www.kouti.com/tables/userattributes.htm