View Full Version : GetElementByID only running first time

09-08-2011, 08:43 PM
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. :mad:

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?

function loadXMLDoc(dname)
if (window.XMLHttpRequest)
xhttp=new XMLHttpRequest();
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
return xhttp.responseXML;

function displayResult()
// code for IE
if (window.ActiveXObject)

// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
lixsltProcessor=new XSLTProcessor();
loxsltProcessor=new XSLTProcessor();


liresultDocument = lixsltProcessor.transformToFragment(xml,document);
loresultDocument = loxsltProcessor.transformToFragment(xml,document);


09-08-2011, 09:15 PM
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.

09-08-2011, 09:52 PM
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?

09-09-2011, 12:04 AM
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:

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:


line in it, while the notloggedin.js file could have the:


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.