需要帮助调试Javascript代码和/或使其工作

时间:2011-05-24 03:35:36

标签: javascript html xml

过去4个小时以来我一直在拔头发。我有两个Javascript文件,两者都可以完全正常工作。一个用作登录验证,另一个用于我的注册页面并将表单写入XML文件。

当我从登录JS中获取一些代码并将其放入我的注册JS时,我的注册JS甚至无法正常运行。我认为我的问题可能是放置我的代码。

如果我在这里发布完整的代码,帖子就像10英尺长,所以这里是我的所有文件: http://www.mediafire.com/?wt9bchq35pdqxgf

顺便说一句,这不是一个真实的应用程序,它只是我正在做的事情。

这是我注册页面的原始Javascript文件:

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var fso = new ActiveXObject("Scripting.FileSystemObject");
var FILENAME = 'C:\\Users\\Wilson Wong\\Desktop\\Copy of Take Home Exam - Copy\\PersonXML2.xml';

function SaveXML(UserData) 
{ 


var file = fso.CreateTextFile(FILENAME, true);


file.WriteLine('<?xml version="1.0" encoding="utf-8"?>\n');
file.WriteLine('<PersonInfo>\n');

for (countr = 0; countr < UserData.length; countr++) 
{
    file.Write('    <Person ');
    file.Write('Usrname="' + UserData[countr][0] + '" ');
    file.Write('Pswd="' + UserData[countr][1] + '" ');
    file.Write('PersonID="' + UserData[countr][2] + '" ');
    file.Write('FirstName="' + UserData[countr][3] + '" ');
    file.Write('LastName="' + UserData[countr][4] + '" ');
    file.Write('Gender="' + UserData[countr][5] + '" ');
    file.Write('DOB="' + UserData[countr][6] + '" ');
    file.Write('Title="' + UserData[countr][7] + '" ');
    file.WriteLine('></Person>\n');
} // end for countr
//file.WriteLine('></Person>\n');

var usrn = document.getElementById("Usrn").value;
var pswd = document.getElementById("Pswd").value;
var pid = document.getElementById("PersonID").value;
var fname = document.getElementById("FirstName").value;
var lname = document.getElementById("LastName").value;
var gender = document.getElementById("Gender").value;
var dob = document.getElementById("DOB").value;
var title = document.getElementById("Title").value;

file.Write('    <Person ');
file.Write('Usrname="' + usrn + '" ');
file.Write('Pswd="' + pswd + '" ');
file.Write('PersonID="' + pid + '" ');
file.Write('FirstName="' + fname + '" ');
file.Write('LastName="' + lname + '" ');
file.Write('Gender="' + gender + '" ');
file.Write('DOB="' + dob + '" ');
file.Write('Title="' + title + '" ');
file.WriteLine('></Person>\n');    

file.WriteLine('</PersonInfo>\n');
file.Close();
} // end SaveXML function --------------------

function LoadXML(xmlFile) 
{
xmlDoc.load(xmlFile);
return xmlDoc.documentElement;
} //end function LoadXML()

function initialize_array() 
{
var person = new Array();
var noFile = true;
var xmlObj;
if (fso.FileExists(FILENAME)) 
{
    xmlObj = LoadXML(FILENAME);
    noFile = false;
} // if
else 
{
    xmlObj = LoadXML("PersonXML.xml");
    //alert("local" + xmlObj);
} // end if

var usrCount = 0;
while (usrCount < xmlObj.childNodes.length) 
{
    var tmpUsrs = new Array(xmlObj.childNodes(usrCount).getAttribute("Usrname"),
                            xmlObj.childNodes(usrCount).getAttribute("Pswd"),
                            xmlObj.childNodes(usrCount).getAttribute("PersonID"),
                            xmlObj.childNodes(usrCount).getAttribute("FirstName"),
                            xmlObj.childNodes(usrCount).getAttribute("LastName"),
                            xmlObj.childNodes(usrCount).getAttribute("Gender"),
                            xmlObj.childNodes(usrCount).getAttribute("DOB"),
                            xmlObj.childNodes(usrCount).getAttribute("Title"));
    person.push(tmpUsrs);
    usrCount++;
}   //end while
if (noFile == false)
    fso.DeleteFile(FILENAME);
SaveXML(person);
}   // end function initialize_array()

这个代码在点击提交按钮后会写入我的XML文件。这就是XML的样子:

<?xml version="1.0" encoding="utf-8"?>

<PersonInfo>

<Person Usrname="Bob111" Pswd="Smith111" PersonID="111" FirstName="Bob" LastName="Smith" Gender="M" DOB="01/01/1960" Title="Hello1" ></Person>

<Person Usrname="Joe222" Pswd="Johnson222" PersonID="222" FirstName="Joe" LastName="Johnson" Gender="M" DOB="12/01/1980" Title="Hello2" ></Person>

<Person Usrname="Tracey333" Pswd="Wilson333" PersonID="333" FirstName="Tracey" LastName="Wilson" Gender="F" DOB="12/01/1985" Title="Hello3" ></Person>

<Person Usrname="Connie444" Pswd="Yuiy444" PersonID="444" FirstName="Connie" LastName="Yuiy" Gender="F" DOB="12/01/1985" Title="Hello4" ></Person>

<Person Usrname="Brian555" Pswd="Dame555" PersonID="555" FirstName="Brian" LastName="Dame" Gender="M" DOB="12/01/1985" Title="Hello5" ></Person>

<Person Usrname="Scott666" Pswd="Bikes666" PersonID="666" FirstName="Scott" LastName="Bikes" Gender="MF" DOB="12/01/1985" Title="Hello6" ></Person>

<Person Usrname="sadsa" Pswd="s" PersonID="s" FirstName="s" LastName="s" Gender="s" DOB="s" Title="s" ></Person>

如果我将代码修改为下面显示的内容,则XML文件甚至不会创建。身份验证也不会正常运行。因为在框中将不会变为红色并且不会弹出警告消息。但是我添加的代码对我登录页面的其他JS文件起作用。

这是编辑后的注册JS:

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var fso = new ActiveXObject("Scripting.FileSystemObject");
var FILENAME = 'C:\\Users\\Wilson Wong\\Desktop\\Copy of Take Home Exam - Copy\\PersonXML2.xml';        
function SaveXML(UserData) 
    {   
var file = fso.CreateTextFile(FILENAME, true);

file.WriteLine('<?xml version="1.0" encoding="utf-8"?>\n');
file.WriteLine('<PersonInfo>\n');

for (countr = 0; countr < UserData.length; countr++) 
{
    file.Write('    <Person ');
    file.Write('Usrname="' + UserData[countr][0] + '" ');
    file.Write('Pswd="' + UserData[countr][1] + '" ');
    file.Write('PersonID="' + UserData[countr][2] + '" ');
    file.Write('FirstName="' + UserData[countr][3] + '" ');
    file.Write('LastName="' + UserData[countr][4] + '" ');
    file.Write('Gender="' + UserData[countr][5] + '" ');
    file.Write('DOB="' + UserData[countr][6] + '" ');
    file.Write('Title="' + UserData[countr][7] + '" ');
    file.WriteLine('></Person>\n');
} // end for countr

var usrn = document.getElementById("Usrn").value;
var pswd = document.getElementById("Pswd").value;
var pid = document.getElementById("PersonID").value;
var fname = document.getElementById("FirstName").value;
var lname = document.getElementById("LastName").value;
var gender = document.getElementById("Gender").value;
var dob = document.getElementById("DOB").value;
var title = document.getElementById("Title").value;

var errmsg = "empty field";
var errmsg2 = "You have register successfully";
var msg = "This user name is already in use";          //this is what I added
var errCount = 0;

errCount += LogInVal(usrn);
errCount += LogInVal(pswd);
errCount += LogInVal(pid);
errCount += LogInVal(fname);
errCount += LogInVal(lname);      //this is what I added
errCount += LogInVal(gender);
errCount += LogInVal(dob);
errCount += LogInVal(title);

if (errCount != 0)                      //the if/else statements are what I added
{
    file.WriteLine('</PersonInfo>\n');      //checks to see if textbox is empty, if yes, alert
    file.Close();
    alert(errmsg);
    return false;
}

else if(authentication(usrn) == true)
{
    file.WriteLine('</PersonInfo>\n');        //checks to see if user name entered is already in use
    file.Close();
    alert(msg);
    return false;    
}

else
{
    file.Write('    <Person ');
    file.Write('Usrname="' + usrn + '" ');
    file.Write('Pswd="' + pswd + '" ');
    file.Write('PersonID="' + pid + '" ');
    file.Write('FirstName="' + fname + '" ');
    file.Write('LastName="' + lname + '" ');       //this block of code here was there originally
    file.Write('Gender="' + gender + '" ');
    file.Write('DOB="' + dob + '" ');              //previous two condition is false, registration successful, writes to XML.
    file.Write('Title="' + title + '" ');
    file.WriteLine('></Person>\n');    

    file.WriteLine('</PersonInfo>\n');
    file.Close();

    alert(errmsg2);
    return true;
}
     } // end SaveXML function --------------------

  function authentication(usrname1)            //function was added
 {
 for (var x = 0; x < arrPerson.length; x++)
 {
    if (arrPerson[x][0] == usrn)
        {
            return true;
        }     
 }
 return false;    
 }

   function LogInVal(objtxt)                //function was added
  {
       if(objtxt.value.length == 0)
{
    objtxt.style.background = "red";
    return 1;
}

else
{
    objtxt.style.background = "white";
    return 0;
}
  }

function LoadXML(xmlFile) 
{
xmlDoc.load(xmlFile);
return xmlDoc.documentElement;
} //end function LoadXML()

function initialize_array() 
{
var person = new Array();
var noFile = true;
var xmlObj;
if (fso.FileExists(FILENAME)) 
{
    xmlObj = LoadXML(FILENAME);
    noFile = false;
} // if
else 
{
    xmlObj = LoadXML("PersonXML.xml");
    //alert("local" + xmlObj);
} // end if

var usrCount = 0;
while (usrCount < xmlObj.childNodes.length) 
{
    var tmpUsrs = new Array(xmlObj.childNodes(usrCount).getAttribute("Usrname"),
                            xmlObj.childNodes(usrCount).getAttribute("Pswd"),
                            xmlObj.childNodes(usrCount).getAttribute("PersonID"),
                            xmlObj.childNodes(usrCount).getAttribute("FirstName"),
                            xmlObj.childNodes(usrCount).getAttribute("LastName"),
                            xmlObj.childNodes(usrCount).getAttribute("Gender"),
                            xmlObj.childNodes(usrCount).getAttribute("DOB"),
                            xmlObj.childNodes(usrCount).getAttribute("Title"));
    person.push(tmpUsrs);
    usrCount++;
}   //end while
if (noFile == false)
    fso.DeleteFile(FILENAME);
SaveXML(person);
}   // end function initialize_array()

这是登录页面JS,其中包含添加到注册JS的代码(在此文件中可以正常工作):

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

//DEFINE LOAD METHOD
function LoadXML(xmlFile)
{
 xmlDoc.load(xmlFile);
 xmlObj = xmlDoc.documentElement;
}

//declare & initialize array
var arrPerson = new Array();

//initialize array w/ xml
function initialize_array()
{
LoadXML("PersonXML.xml");
var x = 0;
while (x < xmlObj.childNodes.length)
{
var tmpArr = new Array(xmlObj.childNodes(x).getAttribute("Usrname"), 
                       xmlObj.childNodes(x).getAttribute("Pswd"), 
                       xmlObj.childNodes(x).getAttribute("FirstName"), 
                       xmlObj.childNodes(x).getAttribute("LastName"), 
                       xmlObj.childNodes(x).getAttribute("DOB"),
                       xmlObj.childNodes(x).getAttribute("Gender"),  
                       xmlObj.childNodes(x).getAttribute("Title"));
arrPerson.push(tmpArr);
x++;   
}
}

//Validation
function LogInVal(objtxt)
{
if(objtxt.value.length == 0)
{
    objtxt.style.background = "red";
    return 1;
}

else
{
    objtxt.style.background = "white";
    return 0;
}
}

//main validation
function MainVal(objForm)
{
var errmsg = "empty field";
var errmsg2 = "Incorrect Username and Password";
var msg = "You have logged in successfully";
var errCount = 0;

var usrn = document.getElementById("usrname1").value;
var pswd = document.getElementById("pswd1").value;

errCount += LogInVal(objForm.usrname);
errCount/*1*/ += LogInVal(objForm.pswd);

initialize_array();    

if (errCount != 0)
{
    alert(errmsg);
    return false;
}
else if(authentication(usrn, pswd) == true)
{

    alert(msg);
    return true;
    setCookie('invalidUsr',' ttttt');
}
else
{
    alert(errmsg2);
    return false;
}
}

function authentication(usrname1, pswd1)
{
for (var x = 0; x < arrPerson.length; x++)
{
    if (arrPerson[x][0] == usrname1 && pswd1 == arrPerson[x][1])
        {
            return true;
        }     
}
return false;    
}

function setCookie(Cookiename,CookieValue)
{
alert('executing setCookie');
document.cookie = Cookiename + '=' + CookieValue;
}

这是我的注册HTML页面:

<html>
<!--onSubmit="SaveXML(person);"-->
<head>
<title>Registration</title>
<link rel="stylesheet" type="text/css" href="CSS_LABs.css" />
</head>


<body>
<script type="text/javaScript"  src="writeXML.js"> </script>

<div class="form">
<form id="Registration" name="reg" action="" method="get" onSubmit="return initialize_array()">

Username:<input type="text" name="Usrn"  id="Usrn" maxlength="10"/> <br/>
Password:<input type="password" name="Pswd" id="Pswd" maxlength="20"/> <br/>

<hr>

PersonID:<input type="text" name="PersonID" id="PersonID"/> <br>

<hr>

First Name:<input type="text" name="FirstName" id="FirstName"/> <br>
Last Name:<input type="text" name="LastName" id="LastName"/>

<hr>

DOB:<input type="text" name="DOB" id="DOB"/> <br>

<hr>

Gender:<input type="text" name="Gender" id="Gender"/> <br>

<hr>

Title:<input type="text" name="Title" id="Title"/> <br>

<hr>

<!--Secret Question:<br>
<select name="secret?">
</select> <br>

Answer:<input type="text" name="answer" /> <br> <br>-->

<input type="submit" value="submit" />

</form>
</div>

</body>

</html>

希望我不会太困惑。

2 个答案:

答案 0 :(得分:0)

我在代码中看到的是:

create XML file, start with <personInfo>
if error, skip </personInfo>
now add something else.

所以你没有关闭XML元素。当然它不会创建文件。它不会写出无效的XML,这是“预期的”行为。

答案 1 :(得分:0)

您还可以调试jvascript代码以启用javascript调试。转到:工具&gt; intenet选项&gt;高级&gt;浏览和取消选中(禁用脚本调试)。在 Internet Explorer浏览器中。然后你可以通过编写调试器; @在javascript函数中的任何位置附加调试器egs: 功能SaveXML(UserData)
 {     调试器;     var file = fso.CreateTextFile(FILENAME,true); file.WriteLine( '\ n');     file.WriteLine( '\ n');
......................... }