...

View Full Version : Need some major javascript help



phenom01
12-11-2011, 10:15 PM
My professor asked us to make a webpage that has a user register and login in via a xml document. We can omly use html, javascrip and css. I made a function that loads users from an xml file and authenticates them. It works only on firefox and not IE. This is a problem because i am using ActiveXobject to write to the file. Any help will greatly be appreciated.

Here is my function that loads xml:


function ReadXML() {

try { xmlDoc = loadMyXML("xmlStudent.xml"); }
catch (objerr) { alert(objerr.description); }
xmlfile = xmlDoc.getElementsByTagName('student');
fname= xmlDoc.getElementsByTagName("firstname");
lname= xmlDoc.getElementsByTagName("lastname");
usernames = xmlDoc.getElementsByTagName("username");
passwords = xmlDoc.getElementsByTagName("password");


}


Here is the xmlfile


<?xml version="1.0" encoding="utf-8"?>
<!--My first xml file (^-^) -->
<CST2309>
<student>
<firstname>bob</firstname>
<lastname>harris</lastname>
<username>admin111</username>
<password>11111111</password>
</student>
<student>
<firstname>joe</firstname>
<lastname>smith</lastname>
<username>joe222</username>
<password>22222222</password>
</student>
<student>
<firstname>sarah</firstname>
<lastname>diaz</lastname>
<username>sarah333</username>
<password>33333333</password>
</student>
<student>
<firstname>katie</firstname>
<lastname>adams</lastname>
<username>katie444</username>
<password>44444444</password>
</student>
</CST2309>

Old Pedant
12-12-2011, 12:19 AM
Try this way of loading XML, instead of what you are using:


function processXML()
{
// this is easiest and fastest way to get XML from a file:
var xml;
if (window.XMLHttpRequest)
{
xml = new XMLHttpRequest();
} else {
// code used only by IE6, IE5
xml = new ActiveXObject("Microsoft.XMLHTTP");
}
xml.open("GET","xmlStudent.xml",false);
xml.send();
var xmlDoc = xml.responseXML;
var e = xmlDoc.documentElement;

var students = xmlDoc.getElementsByTagName("student");
for ( var s = 0; s < students.length; ++s )
{
var student = students[s];
fname= student.getElementsByTagName("firstname")[0].childNodes[0].nodeValue;
lname= student.getElementsByTagName("lastname")[0].childNodes[0].nodeValue;
username= student.getElementsByTagName("username")[0].childNodes[0].nodeValue;
password = student.getElementsByTagName("password")[0].childNodes[0].nodeValue;

// process one student's info here...
....

} // end of for loop

} // end of function

phenom01
12-12-2011, 12:26 AM
It doesnt work. It says "Access denied".

Old Pedant
12-12-2011, 01:14 AM
On what line? From what function?

phenom01
12-12-2011, 01:24 AM
On what line? From what function?

i used a try/catch on the entire code you posted by im guessing its when loading the actual xml file. My code works on firefox but not on IE.

Old Pedant
12-12-2011, 01:38 AM
So get rid of the try/catch so you don't have to guess.

phenom01
12-12-2011, 01:53 AM
So get rid of the try/catch so you don't have to guess.

it fails at this line:



xml.open("GET", "xmlStudent.xml", false);

Old Pedant
12-12-2011, 01:57 AM
Weird. I don't see why. By definition, that file has to be on the same site and in the same directory as your HTML code, since you didn't specify otherwise in the URL.

I'm sorry, I have no idea why you'd get "access denied" from that. I have used that *exact* same coding successfully several times. (And have even sometimes had the XML in other directories, though of course it does have to be on the same site.)

phenom01
12-12-2011, 02:05 AM
Weird. I don't see why. By definition, that file has to be on the same site and in the same directory as your HTML code, since you didn't specify otherwise in the URL.

I'm sorry, I have no idea why you'd get "access denied" from that. I have used that *exact* same coding successfully several times. (And have even sometimes had the XML in other directories, though of course it does have to be on the same site.)

Did it work on IE? I researched this problem for 11 hours yesterday and people are saying that for security reason, Microsoft doesn't let access files. Its weird because it works on firefox. Anyhow, the only way javascript can write to files is through IE correct?

low tech
12-12-2011, 03:04 AM
Hi

Using IE, I get access denied if i try to run it on pc (not via server),

but running it on the server (I use xampp) it works perfectly.

LT

Logic Ali
12-12-2011, 03:05 AM
Did it work on IE? I researched this problem for 11 hours yesterday and people are saying that for security reason, Microsoft doesn't let access files.

I ran a shorter test and discovered that I.E. threw the access denied error under the file:/// protocol but allowed access under http://.

phenom01
12-12-2011, 04:03 AM
I ran a shorter test and discovered that I.E. threw the access denied error under the file:/// protocol but allowed access under http://.

im sorry i dont unserstand what you are saying. Do i need to change something in my code?

Logic Ali
12-12-2011, 04:32 AM
im sorry i dont unserstand what you are saying. Do i need to change something in my code?
No - you need to run your code from a server that uses the http protocol. That means that in the address bar http:// must appear at the start. That will be the case at your college, but on your own computer it's probably file:///.
You can test your code by uploading it to any webspace that you control. Alternatively you can install on your computer a free server like Apache or something else suited to your platform.
This will become neccessary anyway when you progress to developing server-side code.

Old Pedant
12-12-2011, 07:20 AM
Yes, LogicAli and LowTech are correct! I should have remembered that, but I *always* use HTTP: so didn't tumble to it.

You need to be running a web server on your computer, or on the computer where you try this. You can run IIS (Microsoft's web server that is built into all versions of Windows *except* the basic home versions) or, as LowTech said (well,sort of said) you can run an Apache web server on most any machine.

If you don't have a web server and don't have time or a way to set one up, the code should work if you run it on the schools' computers and use their web server.

phenom01
12-13-2011, 02:13 AM
ok i installed the IIS manager. SO i ran the manager, what do i do next? Does it work automatically or do i need to configure something?

DaveyErwin
12-13-2011, 01:06 PM
this will read your xml
from the file system in
both IE and firefox
no server necessary....
(also works if files are on server)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="daveyerwin">
<title>Untitled</title>
<script type="text/javascript">
var xmlhttp=false;
var ie = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
ie=true;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}
xmlhttp.open("GET","xmlStudent.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
if(ie){
xmlDoc = new ActiveXObject ("microsoft.xmldom");
xmlDoc.loadXML(xmlhttp.responseText);
}
alert(xmlDoc.getElementsByTagName("student")[0].getElementsByTagName("lastname")[0].childNodes[0].nodeValue)
</script>
</head>
<body>
<p></p>
</body>
</html>

Old Pedant
12-13-2011, 11:06 PM
I think he means "where do I put my HTML file and then how do I access it."

For starters, just put your HTML and XML files into the directory c:\inetpub\wwwroot

(Later, you could set up other virtual directories, use subdirectories, etc.)

Then you should be able to get the HTML file using the URL http://localhost/myfile.html (where "myfile" is of course replace with the actual file name).

phenom01
12-14-2011, 06:56 AM
I tried it at my school and it saids access denied as well.

Old Pedant
12-14-2011, 08:15 AM
If you didn't use http://... at the beginning of the URL in your browser, then you were *not* using the server properly. So if you did it at school without http://... you proved nothing.

I don't know what else to tell you.

Except look here:
http://www.plopon.com/phenom.html

You can click on the VIEW menu of your browser and then on the SOURCE (or PAGE SOURCE) menu item to see the HTML.

And, yes, that is your xml file, 100% unaltered.

(I wouldn't ordinarily use document.write, but I did so here for simplicity's sake. And do please try this in any browser you choose.)

phenom01
12-14-2011, 04:21 PM
i think he means "where do i put my html file and then how do i access it."

for starters, just put your html and xml files into the directory c:\inetpub\wwwroot

(later, you could set up other virtual directories, use subdirectories, etc.)

then you should be able to get the html file using the url http://localhost/myfile.html (where "myfile" is of course replace with the actual file name).

omg omg i fricking love youuuu. It works!!!!!!!!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum