Javascript 函数没有获取 Windows 用户名

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

最佳答案

虽然嵌套framesetbody不允许,在“旧时代”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

关于Javascript 函数没有获取 Windows 用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11000884/

相关文章:

javascript - 如何改变状态?

javascript - 用于 JS 事件时 <a href ="?"> 的理想值?

html - 表格行的 IE 间距问题

c++ - 如何避免控制台输出有关不支持的操作? (IWebBrowser2,DISP_E_MEMBERNOTFOUND)

c++ - 扩展现有 ActiveX/COM 组件的正确方法是什么?

javascript - 如何在 Ajax 错误上抛出 404 错误以定向到默认 404 行为?

javascript - 如何限制我的所有应用程序中的所有文本框条目 - jquery

html - CSS header div 在 IE 中不会左对齐,但在 Chrome 和 Firefox 中有效

internet-explorer - Internet Explorer的内容安全策略(CSP)解决方法

c# - axVLCPlugin(VideoLAN) 更快的状态到正常状态