Jump to content

Why Is this Null


hybrid kill3r

Recommended Posts

So I have a Javascript function that calls an ajax file. This function has two parameters, url, and div. This function calls the statechange function in which the div variable is passed to. In firebug I get an error saying that the div variable is null on one line, but not on other lines that it is used. Here is my source code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Site Name - Official Website(Beta)</title><link rel='stylesheet' href='template/style.css' />				<script type='text/javascript'>window.onload = loadAjax('http://localhost/ajaxtest.php', 'test');		function loadAjax(url, divid){					if(window.XMLHttpRequest){				xmlhttp = new XMLHttpRequest();			} else {				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");			}			xmlhttp.onreadystatechange = stateChange(divid);			xmlhttp.open("GET",url,true);			xmlhttp.send();					}									function stateChange(divid){						var area = document.getElementById(divid );								if (xmlhttp.readyState==4 && xmlhttp.status==200){					 area.innerHTML=xmlhttp.responseText;				} else if (xmlhttp.readyState == 4 && xmlhttp.status == 404){					area.innerHTML = "";				}else if(xmlhttp.readyState != 4){							area.innerHTML = '<img src="http://localhost/ceasarssoldiers/template/images/ajax-loader.gif">';														}											}			</script></head><body><div id='container'>	<div id='header'>			<a href='http://example.com/index.php' title='Home'><img src='http://localhost/template/images/banner_logo.png' width='543' height='77' alt="Ceasar's Soldiers.com - Official website beta." border='0' /></a>			</div>		<div id='navigation'>				<ul>			<li><a href='#'>Home</a></li>			<li><a href='#'>Forums</a></li>			<li><a href='#'>Rosters</a></li>			<li><a href='#'>Matches</a></li>			<li><a href='#'>Challenge</a></li>			<li><a href='#'>Enlist</a></li>			<li><a href='#'>Login</a></li>			<li><a href='#'>Support</a></li>		</ul>			</div>		<div id='test'>asdf</div>

Error Console in Firebug says that Area is null on line 33 which is this:

							area.innerHTML = '<img src="http://localhost/ceasarssoldiers/template/images/ajax-loader.gif">';

The purpose of that line is to display the ajax loading image while the page is being loaded. Why is Area null on this line, but not the others?

Link to comment
Share on other sites

window.onload = loadAjax('http://localhost/ajaxtest.php', 'test');

This looks like you are assigning the loadAjax function to window.onload. It is not. The script is actually executing loadAjax as soon as it reads this line (before the load event) and assigning the return value of loadAjax to window.onload. Your statefunction is called immediately, with a readyState value less than 4, simply to indicate its progress.This means that when statefunction executes the first time, the browser has not yet read the HTML that creates the 'test' div. So area is undefined. By the time you get a response from your server (readystate == 4) the page has been fully read, and area is defined.You could rewrite the onload assignment a bunch of different ways, but the easiest to understand is to just put it all in quotes, like this:

window.onload = "loadAjax('http://localhost/ajaxtest.php', 'test');"

This assigns the string to window.onload, and the string will be evaluated as code when the load event is fired.

Link to comment
Share on other sites

That fixed that error, but now I'm getting a strange one. Never seen this before but this is the error I get:

Error: uncaught exception: [Exception... "Component returned failure code: 0x805e000a [nsIXMLHttpRequest.open]" nsresult: "0x805e000a (<unknown>)" location: "JS frame :: http://localhost/sitename/index.php :: loadAjax :: line 18" data: no]
Line 18 is:
xmlhttp.open("GET",destination,true);

By the way, I did a google search on the error and found that some people were getting this because of an ad blocker and fixed it by changing some of the variable names. I, however, do not have AdBlock installed at all so I don't see why changing variable names would helped. I tried changing them all but it didn't fix anything.

Link to comment
Share on other sites

Not sure if this will solve your issue or not but you also have another error similar to the one DD pointed out:xmlhttp.onreadystatechange = stateChange(divid);I'll show you a different way to fix this than the one DD showed you (I got this technique from DD, too, BTW)Wrap the function call in what is known as an anonymous function or lambda function, like so:xmlhttp.onreadystatechange = function() { stateChange(divid); }

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...