Javascript function not getting Windows username

function getWindowsUserName()
{
    var WinNetwork = new ActiveXObject("WScript.Network");
    var urlToSite = "http://localhost/index.php?nph-psf=0&HOSTID=AD&ALIAS=" + WinNetwork.UserName;      
    window.frames["psyncLink"].src = "http://localhost/index.php?nph-psf=0&HOSTID=AD&ALIAS=" + WinNetwork.UserName;
    return;
}

I am trying to make the frame load the urlToSite

<body onload="getWindowsUserName()">
    <frameset cols="300px, *"> 
        <frame src="topo1.htm" name="topo" id="topo" application="yes" /> 
        <frame src="topo1.htm" name="psyncLink" id="psyncLink" application="yes" /> 
    </frameset> 
</body>

Actually now I am just getting a blank page. If i visit the same site in IE and manually type the username (case is not sensitive) then the page loads in IE. Therefore I think it's something in the code thats the issue


<html>
    <head>
    <title>AIDS (Automated ID System)</title>
    <HTA:APPLICATION 
    id="frames" 
    border="thin" 
    caption="yes" 
    icon="http://www.google.com/favicon.ico" 
    showintaskbar="yes" 
    singleinstance="yes" 
    sysmenu="yes" 
    navigable="yes" 
    contextmenu="no" 
    innerborder="no" 
    scroll="auto" 
    scrollflat="yes" 
    selection="yes" 
    windowstate="normal" />

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

    function getWindowsUserName()
    {
        var WinNetwork = new ActiveXObject("WScript.Network");
        var urlToSite = createCustomURL(WinNetwork.UserName);
        document.getElementById("psyncLink").src = urlToSite;
    }

    function createCustomURL(userName)
    {
        var customURL = "http://localhost/index.php?nph-psf=0&HOSTID=AD&ALIAS=" + userName;
        return customURL;
    }

</script>

    </head> 
    <body onload="getWindowsUserName()">
        <frameset cols="300px, *"> 
            <frame src="topo1.htm" name="topo" id="topo" application="yes" /> 
            <frame src="topo1.htm" name="psyncLink" id="psyncLink" application="yes" /> 
        </frameset> 
    </body>
</html>

Answers


Though nested frameset in body is not allowed, in "old-days" body element was included after frameset for those browsers which didn't support frames. This still works in IE9 Standards -mode, but then you can't see frames.

To execute getWindowsUserName() after the page has been loaded, you can do something like this:

   window.onload=getWindowsUserName;
</script>
</head>
<frameset cols="300,*">
   <frame src="" name="topo" ...>
   <frame src="topo1.htm" name="psyncLink" ...>
</frameset>

or maybe move getWindowsUserName() to topo1.htm.

More info of frameset in MSDN


A couple of problems:

  • JavaScript uses + for concatenation, not &
  • Property names are case sensitive. Try WinNetwork.UserName
  • You're trying to set the src attribute of the frame window, that doesn't exist. You need to set the src of the frame DOM object. That is, window.frames returns a Window object and document.getElementById('') returns a reference to the HTMLFrameElement
  • (From Teemu) You cannot have a frameset and a body tag on the same page.

Code

var urlToSite = "http://localhost/index.php?nph-psf=0&HOSTID=AD&ALIAS=" +
                 encodeURIComponent(WinNetwork.UserName);


document.getElementById("psyncLink").src = urlToSite;

Reference http://www.pctools.com/guides/scripting/detail/108/?act=reference


Need Your Help

"use strict" only in debug?

javascript minify use-strict

I wonder if there is really nessesary to include the "use strict" when I am done programming and release my JavaScript document to anyone to see. I like to use it because to check that I have coded...

How is nice cpu percentage calculated, e.g. in top?

linux cpu nice

My research group is sharing time on a CentOS server, and we've been using renice +15 to try to lower the priority of long-running background tasks. When running top, these processes do show up as ...