Load results script

Discussion in 'Programming' started by John84, Feb 7, 2007.

  1. #1
    Before I go on, lemme just say that I don't know squat about javascript. That being said...

    I am using the following javascript to load results onto my page...

    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;
    HTML:
    problem is, the results are loading at the bottom of my page. Is there a way to make it load in a specified div?
     
    John84, Feb 7, 2007 IP
  2. John84

    John84 Active Member

    Messages:
    1,288
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    80
    #2
    ive narrowed it down to this portion...

    function resultElement () {
    	if (!document.getElementById('result')) {
    		result = document.createElement('div');
    		result.id = 'result';
    		document.body.appendChild(result);
    	}
    }
    
    HTML:
    ... instead of creating a div I would like it to load on an existing div

    Test page is here
     
    John84, Feb 7, 2007 IP
  3. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #3
    change the function resultElement to following

    function resultElement () {
       return;
        }
    }
    Code (markup):
    Now just make the id of that div "result" where you wants results to be displayed.

    <div id="result">This is my existing div</div>
    Code (markup):
    Not tested, but it should work.
    Thanks.
     
    designcode, Feb 7, 2007 IP