如何使以下代码可移植到 Internet Explorer 以外的浏览器?

发布于 2024-10-09 00:55:33 字数 3901 浏览 2 评论 0原文

我的源代码在 Internet Explorer 上运行良好,但在 Chrome 或 Firefox 上运行失败。我想让它便携。请帮忙。

<html> 
  <head>
   <title>Login</title>

   <script language="javascript">

    function valUser(){

    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.onreadystatechange = functory;
        xmlDoc.load("normal.xml");

    }

    function functory(){
        var u = document.forms[0].user.value;
        var p = document.forms[0].pwd.value;        

        if(xmlDoc.readyState == 4){

            xmlObj = xmlDoc.documentElement;
            var len = xmlObj.childNodes.length;

            for(i = 0; i < len; i++)
            {    
                var nodeElement = xmlObj.childNodes[i];
                var ux = nodeElement.childNodes[0].firstChild.nodeValue;
                var mx = nodeElement.childNodes[1].firstChild.nodeValue;
                var ex = nodeElement.childNodes[2].firstChild.nodeValue;
                var px = nodeElement.childNodes[3].firstChild.nodeValue;

                if((ux == u)&&(px == p)){
                userDetails(ux,mx,ex)
                }
                else {
                var divInvalid = document.getElementById('invalid');
                divInvalid.innerHTML = "<b>Invalid Username or password</b>";
                }
            }
        }

    }

function userDetails(u,m,e){
var newWindow = window.open("", "_self", "height=250,width=250,toolbar=yes,scrollbar=yes,status=yes");
var content = "<html><head><title>Intro</title></head><h1 align='center'><font face='Lucida Handwriting' color='red'>Welcome To GlenMark Pharma</h1></font>";
content += "<body>";
content += "<table align='center'border='1'><tr><th>ENAME</th><th>Mobile</th><th>Email</th></tr>";
content += "<tr><td>"+u+"</td><td>"+m+"</td><td>"+e+"</td></tr></table>";
content += "<div style='postion:absolute;top:10px;right:5px'><a href='logout.html'>LOgout</a></div>";
content += "</body></html>";

newWindow.document.write(content);
newWindow.blur();
}


   </script>
  </head>

  <body >  
   <div style="position:absolute;left:0px;top:0px;">
    <img src="login.jpg" height="650"></img>      

   <div style="font-family:Monotype Corsiva;position:absolute;left:640px;top:250px;"><h1><font color="0066FF">Employee Login</font></h1></div>
   </div>


  <form method="post" action="ret.html" name="frmGlenMark">

   <div style="position:absolute;left:640px;top:325px">
    <input type="text" name="user" size="25">
   </div>

   <div style="position:absolute;left:640px;top:355px">
    <input type="password" name="pwd" size="25">
   </div>

   <div style="position:absolute;left:640px;top:385px">
    <input type="Button" value="Login" onClick="valUser()">
   </div>


   <div id="invalid" style="position:absolute;left:640px;top:410px"></div>

<!--
   <div style="position:absolute;left:705px;top:385px">
    <input type="reset" name="Reset">
   </div>
-->


  </form>
  </body>

</html>

未捕获的引用错误:ActiveXObject 未定义 -->这是我得到的错误。

我已尝试以下方法使其可移植,但仍然出现错误。 例如:

function valUser(){

    if(window.XMLHttpRequest)
    {
        xhttp = new XMLHttpRequest();

    }
    else
    {
          xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

        xhttp.open("GET","normal.xml",false);
        xhttp.send();
        xmlDoc=xhttp.responseXML;
        onreadystatechange = functory;

    }

Uncaught TypeError: DOM 对象构造函数不能作为函数调用。

I have a source code that works well on Internet Explorer,but fails to work on Chrome or Firefox. I want to make it portable.Please Help.

<html> 
  <head>
   <title>Login</title>

   <script language="javascript">

    function valUser(){

    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.onreadystatechange = functory;
        xmlDoc.load("normal.xml");

    }

    function functory(){
        var u = document.forms[0].user.value;
        var p = document.forms[0].pwd.value;        

        if(xmlDoc.readyState == 4){

            xmlObj = xmlDoc.documentElement;
            var len = xmlObj.childNodes.length;

            for(i = 0; i < len; i++)
            {    
                var nodeElement = xmlObj.childNodes[i];
                var ux = nodeElement.childNodes[0].firstChild.nodeValue;
                var mx = nodeElement.childNodes[1].firstChild.nodeValue;
                var ex = nodeElement.childNodes[2].firstChild.nodeValue;
                var px = nodeElement.childNodes[3].firstChild.nodeValue;

                if((ux == u)&&(px == p)){
                userDetails(ux,mx,ex)
                }
                else {
                var divInvalid = document.getElementById('invalid');
                divInvalid.innerHTML = "<b>Invalid Username or password</b>";
                }
            }
        }

    }

function userDetails(u,m,e){
var newWindow = window.open("", "_self", "height=250,width=250,toolbar=yes,scrollbar=yes,status=yes");
var content = "<html><head><title>Intro</title></head><h1 align='center'><font face='Lucida Handwriting' color='red'>Welcome To GlenMark Pharma</h1></font>";
content += "<body>";
content += "<table align='center'border='1'><tr><th>ENAME</th><th>Mobile</th><th>Email</th></tr>";
content += "<tr><td>"+u+"</td><td>"+m+"</td><td>"+e+"</td></tr></table>";
content += "<div style='postion:absolute;top:10px;right:5px'><a href='logout.html'>LOgout</a></div>";
content += "</body></html>";

newWindow.document.write(content);
newWindow.blur();
}


   </script>
  </head>

  <body >  
   <div style="position:absolute;left:0px;top:0px;">
    <img src="login.jpg" height="650"></img>      

   <div style="font-family:Monotype Corsiva;position:absolute;left:640px;top:250px;"><h1><font color="0066FF">Employee Login</font></h1></div>
   </div>


  <form method="post" action="ret.html" name="frmGlenMark">

   <div style="position:absolute;left:640px;top:325px">
    <input type="text" name="user" size="25">
   </div>

   <div style="position:absolute;left:640px;top:355px">
    <input type="password" name="pwd" size="25">
   </div>

   <div style="position:absolute;left:640px;top:385px">
    <input type="Button" value="Login" onClick="valUser()">
   </div>


   <div id="invalid" style="position:absolute;left:640px;top:410px"></div>

<!--
   <div style="position:absolute;left:705px;top:385px">
    <input type="reset" name="Reset">
   </div>
-->


  </form>
  </body>

</html>

Uncaught ReferenceError: ActiveXObject is not defined
--> this is the error I get.

I have tried the following to make it portable but I still get errors.
like:

function valUser(){

    if(window.XMLHttpRequest)
    {
        xhttp = new XMLHttpRequest();

    }
    else
    {
          xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

        xhttp.open("GET","normal.xml",false);
        xhttp.send();
        xmlDoc=xhttp.responseXML;
        onreadystatechange = functory;

    }

Uncaught TypeError: DOM object constructor cannot be called as a function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏至、离别 2024-10-16 00:55:33
function loadXMLDocErr(dname)
{
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.load(dname); 

  if (xmlDoc.parseError.errorCode != 0)
    {
    alert("Error in line " + xmlDoc.parseError.line +
    " position " + xmlDoc.parseError.linePos +
    "\nError Code: " + xmlDoc.parseError.errorCode +
    "\nError Reason: " + xmlDoc.parseError.reason +
    "Error Line: " + xmlDoc.parseError.srcText);
    return(null);
    }
  }
catch(e)
  {
  try //Firefox
    {
    xmlDoc=document.implementation.createDocument("","",null);
    xmlDoc.async=false;
    xmlDoc.load(dname);
    if (xmlDoc.documentElement.nodeName=="parsererror")
      {
      alert(xmlDoc.documentElement.childNodes[0].nodeValue);
      return(null);
      }
    }
  catch(e) {alert(e.message)}
  }
try
  {
  return(xmlDoc);
  }
catch(e) {alert(e.message)}
return(null);
} 
function loadXMLDocErr(dname)
{
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.load(dname); 

  if (xmlDoc.parseError.errorCode != 0)
    {
    alert("Error in line " + xmlDoc.parseError.line +
    " position " + xmlDoc.parseError.linePos +
    "\nError Code: " + xmlDoc.parseError.errorCode +
    "\nError Reason: " + xmlDoc.parseError.reason +
    "Error Line: " + xmlDoc.parseError.srcText);
    return(null);
    }
  }
catch(e)
  {
  try //Firefox
    {
    xmlDoc=document.implementation.createDocument("","",null);
    xmlDoc.async=false;
    xmlDoc.load(dname);
    if (xmlDoc.documentElement.nodeName=="parsererror")
      {
      alert(xmlDoc.documentElement.childNodes[0].nodeValue);
      return(null);
      }
    }
  catch(e) {alert(e.message)}
  }
try
  {
  return(xmlDoc);
  }
catch(e) {alert(e.message)}
return(null);
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文