Javascript函数没有获得Windows用户名

时间:2012-06-12 16:32:34

标签: javascript internet-explorer activex

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;
}

我正在尝试让框架加载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>

实际上现在我只是得到一个空白页面。如果我在IE中访问同一站点并手动键入用户名(大小写不敏感),则在IE中加载页面。因此,我认为代码中存在问题


<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>

2 个答案:

答案 0 :(得分:1)

有几个问题:

  • JavaScript使用+进行连接,而非&
  • 属性名称区分大小写。试试WinNetwork.UserName
  • 您正在尝试设置框架窗口的src属性,该属性不存在。您需要设置框架DOM对象的src。也就是说,window.frames返回一个Window对象,document.getElementById('')返回对HTMLFrameElement的引用
  • (来自Teemu)您不能在同一页面上拥有框架集和正文标记。

代码

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


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

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

答案 1 :(得分:1)

虽然不允许在frameset中嵌套body,但在body之后,对于那些不支持框架的浏览器,我们会包含frameset元素。这仍然适用于IE9标准模式,但是你看不到帧。

要在加载页面后执行getWindowsUserName(),您可以执行以下操作:

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

或者可能将getWindowsUserName()移至topo1.htm。

frameset in MSDN

的详细信息