Results 1 to 4 of 4

Thread: GetElementByID only running first time

  1. #1
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default GetElementByID only running first time

    Hello, I have been working on a login system using scripts from around the net and my own custom ones. I have everything working perfectly but I wanted the news to display one way when they are logged in and one way when they log out. I modified a script that worked perfectly when I only had one element to modify, but when I changed things around to modify 2 divs it breaks.

    The script works completely if I comment out one of the two "document.getElementById" lines at a time, but when they are both uncommented only the first of the two does anything.

    This has been tested in FF, IE, Opera, Safari, and Chrome with the same results.

    Am I really close, or is there something major I missed?
    Code:
    <script>
    	function loadXMLDoc(dname)
    	{
    		if (window.XMLHttpRequest)
    		  {
    			  xhttp=new XMLHttpRequest();
    		  }
    		else
    		  {
    			  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
    		  }
    		xhttp.open("GET",dname,false);
    		xhttp.send("");
    		return xhttp.responseXML;
    	}
    	
    	function displayResult()
    	{
    		xml=loadXMLDoc("includes/loginpanel/loginnews.xml");
    		xslli=loadXMLDoc("includes/loginpanel/loggedinnews.xsl");
    		xsllo=loadXMLDoc("includes/loginpanel/loggedoutnews.xsl");
    		// code for IE
    		if (window.ActiveXObject)
    		  {
    			  exli=xml.transformNode(xslli);
      			  exlo=xml.transformNode(xsllo);
      			  
      			  document.getElementById("loggedinnews").innerHTML=exlo;
      			  document.getElementById("loggedoutnews").innerHTML=exlo;
    		  }
    		// code for Mozilla, Firefox, Opera, etc.
    		else if (document.implementation && document.implementation.createDocument)
    		  {
    			  lixsltProcessor=new XSLTProcessor();
      			  loxsltProcessor=new XSLTProcessor();
      			  
    			  lixsltProcessor.importStylesheet(xslli);
    			  loxsltProcessor.importStylesheet(xsllo);
    			  
    			  liresultDocument = lixsltProcessor.transformToFragment(xml,document);
      			  loresultDocument = loxsltProcessor.transformToFragment(xml,document);
      			  
      			  document.getElementById("loggedinnews").appendChild(liresultDocument);
      			  document.getElementById("loggedoutnews").appendChild(loresultDocument);
    		  }
    	}
    </script>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The code seems a bit tortured and is doing browser tests based upon a feature that all IE have but that not all IE are restricted to. But that's not the source of the problem you're reporting. To really understand that we would probably have to see the live page.

    However, since you obviously have server side code, you could use that to branch the script and only execute one of the two getElementById statements.

    There are various ways to do that. The most straightforward, as long as the page gets reloaded upon login/logout, would be to have two scripts. One that the server writes out the external tag for when the user is logged in, the other whose tag gets written when the user is logged out.

    If you give the external script the extension of the server side language, or it's on the page, you can even branch inside it, but that can get tricky.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    hobbymaster001 (09-08-2011)

  4. #3
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    I am fairly new to javascript here so it doesn't surprise me that the script is pretty tortured. I have a lot of learning to do. Now, as far as the server side scripts go, it is just a block of php at the top of the file which handles the cookies, password encryption and database checking. More or less within the body it checks to see if they are logged in with php and displays content based on the value.

    The web page in question is www.photosbyrbaden.com/gallery.php

    I have removed the code temporarily so I can see if there would be an easier way to populate an unordered list from an xml file from within the div. Any ideas?

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I can't very well diagnose a problem with code that's not on the page. If you want help with your lists, please start a new thread about that.

    But there is something in your last post that I can sort of work with on the issue of the script. You wrote:

    More or less within the body it checks to see if they are logged in with php and displays content based on the value.
    You could have it do the same with javascript, something like:

    PHP Code:
    <?php
    if($loggedin){
        echo 
    '<script type="text/javascript" src="loggedin.js"></script>';
    }
    else {
        echo 
    '<script type="text/javascript" src="notloggedin.js"></script>';
    }
    ?>
    That can go in the head or in the body, wherever it needs to be. The loggedin.js file could have the:

    Code:
    document.getElementById("loggedinnews").appendChild(liresultDocument);
    line in it, while the notloggedin.js file could have the:

    Code:
    document.getElementById("loggedoutnews").appendChild(loresultDocument);
    line in it. Neither would need both. Otherwise the two scripts could be the same. Or, more likely could have other selective inclusions/exclusions of lines tailored to the login state of the user.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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