Jump to content

Why is this Null?


ShoeLace1291

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.