Results 1 to 5 of 5

Thread: DOM alternative to document.write(xmlhttp.responseText)

  1. #1
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,875
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default DOM alternative to document.write(xmlhttp.responseText)

    1) Script Title: DOM alternative to document.write(xmlhttp.responseText)

    2) Script URL (on DD): http://www.dynamicdrive.com/dynamici...axincludes.htm

    3) Describe problem: The Ajax Includes Script uses document.write(xmlhttp.responseText) (or something similar). What's the most elegant way of converting that to modern DOM-coding?
    Thanks in advance,
    Arie Molendijk.
    Last edited by ddadmin; 08-20-2008 at 10:06 PM.

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    You can try document.getElementById or nodeValue
    Code:
    <script type="text/javascript">
    window.onload=function()
    {
    document.getElementById('elID').innerHTML='This is created by innerHTML.';
    document.getElementById('nodeVal').firstChild.nodeValue='This is created by nodeValue.';
    }
    </script>
    <div id="elID"></div>
    <div id="nodeVal">&nbsp;</div>
    <script type="text/javascript">
    document.write('This is created by document.write()');
    </script>
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. #3
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,875
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Rangana, thanks, but I don't want to use innerHTML and document.write.
    If I do <body id=id='nodeVal'>, then
    document.getElementById('nodeVal').firstChild.nodeValue=xmlhttp.responseText just gives me the string. In order to get the 'real thing', I have to do document.write(document.getElementById('nodeVal').firstChild.nodeValue);
    but I didn't want to use innerHTML from the start.
    So I'm looking now for a method to replace document.write(document.getElementById('nodeVal').firstChild.nodeValue); with something that doesn't use inner and write.
    ===
    Arie.

  4. #4
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    The existing AJAX script, which you are talking about is designed in such a manner that the AJAX call will be made before completely loading the page in which you've integrated this script.

    If you are looking for a fully DOM based method for handling the response from the server. Then I think it would be better if you use either XML or JSON as the response type from the server to the client. After getting the response use JS DOM routine to construct the document part in your page. Also you have to change some changes in the original AJAX script you are using:

    Code:
    var page_request = false;
    function ajaxinclude(url) {
    	if (window.XMLHttpRequest) // if Mozilla, Safari etc
    		page_request = new XMLHttpRequest()
    	else if (window.ActiveXObject) { // if IE
    		try {
    			page_request = new ActiveXObject("Msxml2.XMLHTTP")
    		} catch(e) {
    			try {
    				page_request = new ActiveXObject("Microsoft.XMLHTTP")
    			} catch(e) {}
    		}
    	} else 
    		return false 
    		
    	page_request.open('GET', url, false) //get page synchronously 
    	page_request.send(null) 
    	//writecontent(page_request)
    }
    
    function writecontent(page_request) {
    	/*if (window.location.href.indexOf("http") == -1 || page_request.status == 200) 
    		document.write(page_request.responseText);*/
    }
    I've highlighted the changes I've made.

    Then in your window's load event you can call the JS routine that construct the DOM Tree based on the server response
    Code:
    	window.onload = function(){
    		your_DOM_Constructor_Method(); //You don't have to pass the XHR object as it is a global one and will be accessible inside your method.
    	}
    Here is some good articles about the technique using which you can construct the fully DOM based structure, this articles explains the technique in a step by step manner and can be followed very easily. You can find other good articles about this one.

    http://www.quirksmode.org/blog/archi...ax_respon.html
    http://www.quirksmode.org/blog/archi..._respon_1.html

    Hope this helps.

  5. #5
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,875
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Codeexploiter, thanks. I'll give that a try.
    Arie.

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
  •