Displaying results at the end of the page

Discussion in 'JavaScript' started by sitefever, Sep 16, 2007.

  1. #1
    The following js code is currently working- however, the formatting is incorrect.

    The result is added at the end of the html page (after the </body> tag) instead of within the body.

    I'm thinking it has to do with the line document.body.appendChild(result); but it's been a while since I messed around with javascript. Any ideas??

    var xml = xmlObject();
    var url;
    var button;
    var result;
    function xmlObject () {
    	if (typeof XMLHttpRequest == 'undefined') {
    		objects = Array(
    			'Microsoft.XmlHttp',
    			'MSXML2.XmlHttp',
    			'MSXML2.XmlHttp.3.0',
    			'MSXML2.XmlHttp.4.0',
    			'MSXML2.XmlHttp.5.0'
    		);
    		for (i = 0; i < objects.length; i++) {
    			try {
    				return new ActiveXObject(objects[i]);
    			} catch (e) {}
    		}
    	} else {
    		return new XMLHttpRequest();
    	}
    }
    function resultElement () {
    	if (!document.getElementById('result')) {
    		result = document.createElement('div');
    		result.id = 'result';
    		document.body.appendChild(result);
    	}
    }
    function handleResults () {
    	if (xml.readyState == 4) {
    		if (xml.responseText == 'url.blank') {
    			result.innerHTML = 'Enter a valid URL';
    		} else {
    			result.innerHTML = xml.responseText;
    		}
    	} else {
    		result.innerHTML = 'Loading..';
    	}
    }
    function getResults () {
    	resultElement();
    	xml.open('get', 'results.php?url=' + escape(url.value));
    	xml.onreadystatechange = handleResults;
    	xml.send(null);
    }
    function loadHandler () {
    	url = document.getElementById('url');
    	button = document.getElementById('button');
    	button.onclick = getResults;
    }
    window.onload = loadHandler;
    Code (markup):

     
    sitefever, Sep 16, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Create a <div> with the ID result and place it wherever you want to display the results. That alone should work.

    EDIT:

    Actually, maybe not. Try replacing this.
    
    function resultElement () {
    	if (!document.getElementById('result')) {
    		result = document.createElement('div');
    		result.id = 'result';
    		document.body.appendChild(result);
    	}
    }
    
    Code (javascript):

    With
    
    function resultElement () {
    	if (!document.getElementById('result')) {
    		result = document.createElement('div');
    		result.id = 'result';
    		document.body.appendChild(result);
    	}
        else
        {
            result = document.getElementById('result');
        }
    }
    
    Code (javascript):
    As well...
     
    nico_swd, Sep 16, 2007 IP
  3. sitefever

    sitefever Banned

    Messages:
    782
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    0
    #3
    nico- you're awesome. I know this is not the first time you've helped me out.

    Do you have a site that I can link to on my blog in return for the assistance?
     
    sitefever, Sep 16, 2007 IP