Cocoa Dev
Cocoa Dev

Reputation: 9541

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>

Upvotes: 1

Views: 4223

Answers (2)

Teemu
Teemu

Reputation: 23406

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

Upvotes: 1

Ruan Mendes
Ruan Mendes

Reputation: 92274

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

Upvotes: 1

Related Questions