Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Need some major javascript help

    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:
    Code:
    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
    Code:
    <?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>
    Last edited by phenom01; 12-11-2011 at 09:18 PM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,695
    Thanks
    80
    Thanked 4,511 Times in 4,475 Posts
    Try this way of loading XML, instead of what you are using:
    Code:
    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
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    It doesnt work. It says "Access denied".

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,695
    Thanks
    80
    Thanked 4,511 Times in 4,475 Posts
    On what line? From what function?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    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.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,695
    Thanks
    80
    Thanked 4,511 Times in 4,475 Posts
    So get rid of the try/catch so you don't have to guess.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    So get rid of the try/catch so you don't have to guess.
    it fails at this line:

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

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,695
    Thanks
    80
    Thanked 4,511 Times in 4,475 Posts
    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.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New Coder
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    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?

  • #10
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    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
    Last edited by low tech; 12-12-2011 at 02:12 AM.
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • Users who have thanked low tech for this post:

    phenom01 (12-14-2011)

  • #11
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by phenom01 View Post
    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://.

  • Users who have thanked Logic Ali for this post:

    phenom01 (12-14-2011)

  • #12
    New Coder
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    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?

  • #13
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by phenom01 View Post
    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.

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,695
    Thanks
    80
    Thanked 4,511 Times in 4,475 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #15
    New Coder
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •