AJAX Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    AJAX Error

    I have the below link using the below ajax code and its throwing up the below error, can anyone help, I've been at it for ages and just can't figure it out, thanks for your help in advance.
    Error: document.getEle mentById(contai nerid) is null
    Source File: http://localhost/admin/useradmin.php?f unc=1#
    Line: 69
    Code:
    <a onclick="getcontent('../ajax-111-ca+421/', 'span1'); return false;" href="#">
    Code:
    <script type="text/javascript" language="javascript">
    	var loading_img = '/media/images/layout/loading.gif';
    	var loading_msg = ' Loading...';
    	var xmlhttp_obj = false;
    	
    	function xmlhttp()
    	{
    		if (window.XMLHttpRequest)
    		{ // if Mozilla, Safari etc
    			xmlhttp_obj = new XMLHttpRequest();
    		}
    		else if (window.ActiveXObject)
    		{ // if IE
    			try
    			{
    				xmlhttp_obj = new ActiveXObject("Msxml2.XMLHTTP");
    			}
    			catch (e)
    			{
    				try
    				{
    					xmlhttp_obj = new ActiveXObject("Microsoft.XMLHTTP");
    				}
    				catch (e)
    				{
    
    				}
    			}
    		}
    		else
    		{
    			xmlhttp_obj = false;
    		}
    
    		return xmlhttp_obj;
    	}   //get content via GET
    
    	function getcontent(url, containerid)
    	{
    		var xmlhttp_obj = xmlhttp();
    		document.getElementById(containerid).innerHTML = '<img src="' + loading_img + '" />' + loading_msg;
    		xmlhttp_obj.onreadystatechange=function()
    		{
    			loadpage(xmlhttp_obj, containerid);
    		}
    		xmlhttp_obj.open('GET', url, true);
    		xmlhttp_obj.send(null);
    		alert(url);
    	}     
    
    	function loadpage(xmlhttp_obj, containerid)
    	{
    		if ( xmlhttp_obj.readyState == 4 && xmlhttp_obj.status == 200 )
    		{
    			document.getElementById(containerid).innerHTML = xmlhttp_obj.responseText;
    		}
    	}
    //]]>
    
    </script>
  • unauthorized
    New Member
    • May 2009
    • 81

    #2
    Do this in your browser:
    containerid?
    Shouldn't that be "document.conta inerid"?

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      could you try to trace/alert the containerid? the error is thrown when document.getEle mentById() doesn't find the element and you try to use an object's method or property that doesn't exist ... so currently in fact you try something like:

      Code:
      null.innerHTML = 'foo'
      which throws the error. may be you are passing the wrong id or you don't pass the id correctly? could you post how you do the call?

      kind regards

      Comment

      • ziycon
        Contributor
        • Sep 2008
        • 384

        #4
        Sorted it out, I'm pasing 'span1' into the function but on the webpage i had the span set as:
        Code:
        <span1></span1>
        When it should have been:
        Code:
        <span id="span1"></span>
        Thanks for your help.

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          glad to hear that you found it out :)

          kind regards

          Comment

          Working...