AJAX does not work without an alert

Discussion in 'JavaScript' started by knkk, Dec 19, 2008.

  1. #1
    Instead of an id getting its innerHTML changed, the entire page is getting refreshed with this function of mine (you may want to look just at the end of the function where there's an alert):

    function morerating(ratingform, checkflag, loc)
    {
    	var f = document.forms[ratingform];
    	var rating_done_flag = 0;
    	var params = "";
    	if (checkflag == true)
    		check = checkrating(ratingform);
    	else 
    		check = true;
    	if(check == true)
    	{
    		f.submit_rating_done.value = "";
    		for (i=0;i < f.elements.length;i++) { 
    			if(f.elements[i].type == 'radio' || f.elements[i].type == 'checkbox')
    			{
    				if(f.elements[i].checked)
    				{
    					params = params + f.elements[i].name +'='+ encodeURI(f.elements[i].value) + '&'; 
    					if (f.elements[i].name == "rating_1" || f.elements[i].name == "rating_2" || f.elements[i].name == "rating_3" || f.elements[i].name == "rating_4" || f.elements[i].name == "rating_overall")
    						rating_done_flag = 1;
    				}
    			}
    			else if (f.elements[i].name && f.elements[i].value)
    			{
    				params = params + f.elements[i].name +'='+ encodeURI(f.elements[i].value) + '&';
    			}
    		}
    		shows = loc;
    		xmlHttp=GetXmlHttpObject();
    		if (xmlHttp==null)
    		{
    			alert ("Browser does not support HTTP Request");
    			return;
    		}
    		if(document.getElementById('morehide'))
    		{
    			document.getElementById('morehide').style.display = "none";
    			document.getElementById('morehide').innerHTML = "";
    		}
    		if(shows == "rating_main_block" || shows == "rating_main_block_editorial" || shows == "loginbox" || shows == "editorial_loginbox")
    		{
    			params = params + "submit_rating_done=1&";
    			params = params + "xmlrequest=1&";
    		}
    		else
    		{
    			if(!document.getElementById('morehide'))
    			{
    				f.submit_rating_done.value = "Submit";
    			}
    		}
    		params = params + "shows="+shows;
    		var url='/blocks/profiles/common/user_store.php';
    		url=url + "?" + params;
    		xmlHttp.onreadystatechange=stateChangedALL;
    		xmlHttp.open("GET",url,true);
    		xmlHttp.send(null);
    		//setTimeout('window.scrollBy(0, 350)', 1000);
    		//alert('help2');
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    Code (markup):
    That alert('help2'), if removed, causes the page to just reload, but with it, AJAX works fine. I tried some dummy setTimeout option also as you can see, but that doesn't seem to help either. I also tried setTimeout("alert('help2')", 1000).

    If it helps, these are the other functions referred to there, though they're pretty standard AJAX stuff:

    
    function stateChangedALL()
    {
    	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    	{
    			document.getElementById(shows).style.display="block"
    			if (xmlHttp.responseText != "")
    				document.getElementById(shows).innerHTML=xmlHttp.responseText
    			else
    				document.getElementById(shows).style.display="none"
    			var the_node = document.getElementById(shows);
    			execJS(the_node);
    			if(document.getElementById(trashid))
    			{
    				xmlHttp2.send(null);
    			}
    	}
    	else
    	{
    			document.getElementById(shows).innerHTML="<br><br><div class=\"a9bl\" align=\"center\"><font color=black>Loading, please wait...</font><div>"
    	}
    }
    
    Code (markup):
    and

    
    function GetXmlHttpObject()
    { 
        var req = false;
        // branch for native XMLHttpRequest object
        if(window.XMLHttpRequest && !(window.ActiveXObject)) 
    	{
    		try {
    			req = new XMLHttpRequest();
            } catch(e) {
    			req = false;
            }
        // branch for IE/Windows ActiveX version
        }
    	else if(window.ActiveXObject) 
    	{
           	try {
            	req = new ActiveXObject("Msxml2.XMLHTTP");
          	} catch(e) {
            	try {
              		req = new ActiveXObject("Microsoft.XMLHTTP");
            	} catch(e) {
              		req = false;
            	}
    		}
        }
    	return req
    }
    
    Code (markup):
    I'd be quite grateful for any pointers! Thank you for your time!
     
    knkk, Dec 19, 2008 IP
  2. rene7705

    rene7705 Peon

    Messages:
    233
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Writing your own ajax handlers is like making a wheel with bamboo.. bit dodgy.
    I recommend jQuery.com's ajax routines (15kb, free).
     
    rene7705, Dec 19, 2008 IP
  3. rene7705

    rene7705 Peon

    Messages:
    233
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    But to tell you something about the code you're using now; it's probably a timing issue between the return-handler and the rest of your code, that calls morerating(). something else is likely depending on the result (when caught by stateChangedALL()). the alert ensures that you spend more than 1 second, and that's enough time for the ajax call to complete.
    if this is the case, then the solution is to set a global var busy=true, and have the rest of your code check for that var being set to false (by stateChangedAll()).
     
    rene7705, Dec 19, 2008 IP
  4. knkk

    knkk Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Okay, I got it - I was returning true when I should have been returning false. So this was the issue:

    
    function morerating(ratingform, checkflag, loc)
    {
                    //some code...
    		xmlHttp.onreadystatechange=stateChangedALL;
    		xmlHttp.open("GET",url,true);
    		xmlHttp.send(null);
    		[COLOR="Red"]return false[/COLOR]; /*this was "true" earlier*/
    	}
    	else
    	{
    		return false;
    	}
    }
    
    Code (markup):
    Still, though, why was the alert making it work?
     
    knkk, Dec 22, 2008 IP