www.webdeveloper.com
Results 1 to 5 of 5

Thread: troubles with an if... [ajax]

  1. #1
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    304

    troubles with an if... [ajax]

    Hello fellas,

    This is something I just cant seem to crack up, must be an easy thing but cant spot it...

    Im trying to condition the output of a call to a php file (the call itself works perfectly) so I figured "an if and that would do it", well it doesnt...

    here is my function:
    HTML Code:
    function handleResponse() {
    	    if(http.readyState == 4){
    		var response = http.responseText;
    			
    		document.getElementById("update").innerHTML = response;
    		return response;
    		
    	    }
    	}
    so like that it resets the "update" div and that is ok, but depending on what button made the call I would need not to update this div but another one or perhaps a java applet string (but if I can get it to the other div that would be no trouble to handle the applet thing), so for instance this didnt work:

    HTML Code:
            var http = createRequestObject();
    	var file = "MyFile.php";
    	var doReturn = "false";
    
            function createRequestObject() {
    	    var ro;
    	    var browser = navigator.appName;
    	    if(browser == "Microsoft Internet Explorer"){
    		ro = new ActiveXObject("Microsoft.XMLHTTP");
    	    }else{
    		ro = new XMLHttpRequest();
    	    }
    	    return ro;
    	}
    
           function sndReq(action, param) {
    
    	    http.open("GET", this.file +\'?\' + action + \'=\' + param);
    	    http.onreadystatechange = handleResponse;
    	    http.send(null);
    	}
    
    	function handleResponse() {
    	    if(http.readyState == 4){
    		var response = http.responseText;
    
    		if(this.doReturn == "false") { alert("false"); }			
    		if(this.doReturn == "true") { alert("true"); }
    		
    		document.getElementById("update").innerHTML = response;
    		return response;
    		
    	    }
    	}
    
    
    function mostrarJuego(id)
    {
    	this.doReturn = "true";
    	out = sndReq("gameFromId",id);
    	
    	this.doReturn = "false";
    }
    it doesnt "alert" anything, even with the clause in the handleResponse function suggests it should alert something... it simply ignores it and it works to reset the update div just fine....

    in a sort of pseudo code:
    Code:
    if ( doReturn ) set Div1.innerHTML = response AND return response
    if ( !doReturn) set div2.innerHtml = response
    So any idea you may have will be appreciated.

    thank you for your time
    Please! use the PHP tags! or at least CODE tags
    (Code posted may work )
    --

  2. #2
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    You need to find a better way of creating the object. IE7 does not use the ActiveX unless the person turns off the Native Object in the browser. So you do not want to check for IE. You need to learn about OBJECT detection. Only way to check if a browser supports something!

    this is not going to point towards your object, you loose all that reference when you do the wonderful Ajax call. Remember it is doing an async request.

    You would have to build a closure to keep the this reference. One thing is, I do not think you have the object scope right to even reference doReturn

    Also your doReturn will always be false since you are setting it, making the ASYNCHRONOUS call, and than setting it again.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

  3. #3
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    304
    Thanks Eric for pointing all that out, Ive found how to check for the object itself rather than the browser, this it would go like this:
    Code:
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    So I will try to implement that when I get this working.

    What im not fully understanding is why the alerts get ignored, while the division.innerHTML is working fine, I do need to condition whether to set it to the div "A" or the div "B" so if you could suggest a way of doing it that'd be great!

    Perhaps I should rephrase and say: How can I use an IF inside the handleResponse() function



    Thank you for taking the time to read through
    Please! use the PHP tags! or at least CODE tags
    (Code posted may work )
    --

  4. #4
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    Have you added debug statements? I have a feeling that this is in the wrong scope.

    Think you want to look into: http://java.sun.com/javascript/ajaxi...ction_ApB.html

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

  5. #5
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    304
    wow!! awesome link!! i already started reading it! thanks a lot man!
    Please! use the PHP tags! or at least CODE tags
    (Code posted may work )
    --

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles