SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reloading included file

    does anyone can send me an example how to reload a div?
    I have an asp included file inside a div and i'd like to refresh it..

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is one example quickly thrown together:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    // NOTE: This example uses POST to avoid problems with IE caching GET
    // One problem that POST has is it doesn't handle reloading HTML files well.  If your file is php or asp no problem
    
    var refreshInterval; // variable to store the interval object
    var refreshTime = 60000; // refresh every 60000ms or 1 minute
    var refreshURL = "mypage.asp"; // asp page to load into div
    
    function HTTPReq() {
    
    	var theAJAX;
    
    	if(window.XMLHttpRequest) {
    		theAJAX = new XMLHttpRequest();
    		if(theAJAX.overrideMimeType) {
    			theAJAX.overrideMimeType('text/xml');
    		}
    	} else if(window.ActiveXObject) {
    		try {
    			theAJAX = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch(e){ 
    			try {
    				theAJAX = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch(e) {
    				theAJAX = false;
    			}
    		}
    	}
    	if(!theAJAX){
    		alert('Giving up :( Cannot create an XMLHTTP instance');
    	}
    	return theAJAX;
    }
    
    function doAjaxPost(paramList,url,onsuccess,onfail) {
    
    	var ajax = HTTPReq();
    	if (!ajax) return;
    
    	var paramArr = [];
    	for (var item in paramList) {
    		// don't handle arrays, objects, functions, etc
    		if (typeof paramList[item] == "string" || typeof paramList[item] == "number") {
    			paramArr.push(encodeURIComponent(item) + "=" + encodeURIComponent(paramList[item]));
    		}
    	}
    	var params = paramArr.join("&");
    
    	ajax.open("POST", url, true);
    	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); 
    	ajax.onreadystatechange = function() {
    		if (ajax.readyState == 4) {
    			if (ajax.status == 200 || ajax.status == 0) {
    				onsuccess(ajax.responseText);
    			} else {
    				onfail(ajax.status);
    			}
    		}
    	}
    	ajax.send(params);			
    }
    
    function loadPage() {
    	doAjaxPost({},refreshURL,
    		function (ajaxText) {
    			document.getElementById("refreshMe").innerHTML = ajaxText;
    		}, 
    		function (statusCode) {
    			alert("error loading page, status was " + statusCode);
    		}
    	);
    }
    
    window.onload = function() {
    	refreshInterval = window.setInterval(loadPage,refreshTime);
    }
    </script>
    </head>
    <body>
    <div id="refreshMe">some content I want to refresh</div>
    </body>
    </html>

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok...I'll try

    The window.onload do the call or should I add loadPage() on body onload?

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what I had:
    <div ><!--#include file="slide_menu.asp"--> </div>
    (this worked fine but no refresh..)

    Then I shift to:
    Added your js example;
    var refreshURL = "slide_menu.asp";

    <div id="refreshMe"></div>
    Nothing happened..

    Then I shift again to:

    Added your js example;
    var refreshURL = "<!--#include file="slide_menu.asp"-->";

    <div id="refreshMe"></div>
    (and only now the page was loaded...)


    I'm confuse... can you clarify me about your ajax example and the scenario I described?

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    should be fine with
    refreshURL = "slide_menu.asp"

    it refreshes after 1 minute, you might want to change that setting too

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By the way,
    there's any chance to reach the inputs on the loaded file?
    i tried getelementbyID("elementid") with no luck...


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
  •