SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    create div and place php file content in there

    I have a createtag_hi.php file that simply says hi
    Code:
    function createRequest(){
    	var req = false;
    	try {
    		req = new ActiveXObject('Msxml2.XMLHTTP');
    	}catch (e2){
    		try{
    			req = new ActiveXObject('Microsoft.XMLHTTP');
    	}catch(e3){
    		try{
    			req = new XMLHttpRequest();
    	}catch(e1){
    			req = false;
    			}
    		}
    	}
    	return req;
    }
    function sayHi(){
    	var newDiv = document.createElement("div");
    	newDiv.setAttribute('style', 'float: left; z-index: 3; background-color: #999999;');
    	newDiv.setAttribute('id', 'hiid');
    	var req = createRequest();
    	if(req){
    		req.onreadystatechange = function(){
    			var c = document.getElementById('hiid');
    			if(req.readyState){
    				if(req.readyState == 4){
    					if(req.status == 200){
    						c.innerHTML = req.responseText;
    					}
    				}
    			}
    		}
    
    	//var getKeyURL = document.getElementById('displayurl').innerHTML
    	//document.write(getKeyURL);
    	req.open("GET","createtag_hi.php",true);
    	req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    	req.send(null);
    	}
    }
    I'm getting this error:
    Error: c is null
    Source File: http://...new/twotexts/js/createtag_ajax.js
    Line: 42
    Compare bible texts (and other tools):
    TheWheelofGod

  2. #2
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    document.getElementById won't work if the element has not been injected into the DOM.

    Why not just do:

    Code JavaScript:
    newDiv.innerHTML = req.responseText;
    // And then inject it:
    document.body.appendChild(newDiv);
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  3. #3
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It worked thanks!
    Compare bible texts (and other tools):
    TheWheelofGod


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
  •