dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Problem with Ajax on IE when Page is reloaded

  1. #1
    Join Date
    Jan 2009
    Posts
    4

    Question Problem with Ajax on IE when Page is reloaded

    Hi,

    I am trying to load some data using Ajax, it works fine when I open the page for the first time, but, when I reload the page it doesn't display any output...

    there are approximate 10 such function and it is happening with every function, but it is working with on Firefox...


    this is my function to create HTTP object:

    Code:
    function getHTTPObject()
    {
    
    
    
    	try
    	{
    		return new XMLHttpRequest();
    	}
    	catch (e)
    	{
    	  // Internet Explorer
    		try
    		{
    			return  new ActiveXObject("Msxml2.XMLHTTP");
    		}
    		catch (e)
    		{
    			try
    			{
    			  return  new ActiveXObject("Microsoft.XMLHTTP");
    			}
    			catch (e)
    			{
    				 alert("Your browser does not support AJAX!");
    				 return false;
    			}
    		}
    	  }  
    }

    Code:
    here is the function:
    
    
    function loadQuickLink(fid)
    {
    	//alert(fid);
    	if(xmlHttp==null)
    	{
    		xmlHttp=getHTTPObject();
    	}
    	xmlHttp.open("GET",url+"?do=loadQuickLink&fid="+fid,true);
    	xmlHttp.send(null);	
    	
    	xmlHttp.onreadystatechange=showQuickMenu;
    }
    and here is the response handler:


    Code:
    function showQuickMenu()
    {
    	alert(xmlHttp.responseText);
    //	if(xmlHttp.responseText.length!=0)
    //	{
    		document.getElementById('quick_post_link').style.display="block";
    		document.getElementById('quick_post_link_').style.display="block";
    		//document.getElementById('quick_post_link').className='vbmenu_control';
    		document.getElementById('quick_post_link_').className='vbmenu_control';
    		document.getElementById('quick_post_link').innerHTML=xmlHttp.responseText;
    //	}
    }


    as i said It's working fine when i open the page, but doesn't work properly after page refresh...

    code seems to be fine caz it's workign fine on Firefox, please help me with this...

  2. #2
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    well I'm assuming that "xmlHttp" is global and defined outside that all ...

    first solution try replacing this:
    if(xmlHttp==null)
    {
    xmlHttp=getHTTPObject();
    }
    by:
    xmlHttp=null;
    xmlHttp=getHTTPObject();

    or even why not having ur code like that:

    Code:
    function loadQuickLink(fid)
    {
    	//alert(fid);
            var xmlHttp=null;
    	try
    	{
    		xmlHttp=new XMLHttpRequest();
    	}
    	catch (e)
    	{
    	  // Internet Explorer
    		try
    		{
    			xmlHttp= new ActiveXObject("Msxml2.XMLHTTP");
    		}
    		catch (e)
    		{
    			try
    			{
    			  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    			}
    			catch (e)
    			{
    				 alert("Your browser does not support AJAX!");
    			}
    		}
    	  }  
    
    	xmlHttp.open("GET",url+"?do=loadQuickLink&fid="+fid,true);
    	xmlHttp.send(null);	
    	
    	xmlHttp.onreadystatechange=showQuickMenu;
    }
    if u r looking for a generalized AJAX function pass the url and the function name as paramter to the abaove function ...
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  3. #3
    Join Date
    Jan 2009
    Posts
    4

    Lightbulb

    Quote Originally Posted by Amahdy View Post
    well I'm assuming that "xmlHttp" is global and defined outside that all ...

    first solution try replacing this:
    if(xmlHttp==null)
    {
    xmlHttp=getHTTPObject();
    }
    by:
    xmlHttp=null;
    xmlHttp=getHTTPObject();

    or even why not having ur code like that:

    Code:
    function loadQuickLink(fid)
    {
    	//alert(fid);
            var xmlHttp=null;
    	try
    	{
    		xmlHttp=new XMLHttpRequest();
    	}
    	catch (e)
    	{
    	  // Internet Explorer
    		try
    		{
    			xmlHttp= new ActiveXObject("Msxml2.XMLHTTP");
    		}
    		catch (e)
    		{
    			try
    			{
    			  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    			}
    			catch (e)
    			{
    				 alert("Your browser does not support AJAX!");
    			}
    		}
    	  }  
    
    	xmlHttp.open("GET",url+"?do=loadQuickLink&fid="+fid,true);
    	xmlHttp.send(null);	
    	
    	xmlHttp.onreadystatechange=showQuickMenu;
    }
    if u r looking for a generalized AJAX function pass the url and the function name as paramter to the abaove function ...

    I can do that, but it doesn't make sense caz I've 7 such functions performing different type of Ajax request and each handling Ajax Response differently...
    Creating
    Code:
    xmlHttp
    object in each function will duplicate the same code and make my code 7 times lengthy

    hey Amahdy can you tell me what can be reason for this weird behavior?

  4. #4
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    Quote Originally Posted by ravish.tiwari
    will duplicate the same code and make my code 7 times lengthy
    have u considered this:
    Quote Originally Posted by amahdy
    if u r looking for a generalized AJAX function pass the url and the function name as paramter to the abaove function ...
    does it works for u just fine now? did u tried the first solution?
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  5. #5
    Join Date
    Jan 2009
    Posts
    4
    Quote Originally Posted by Amahdy View Post
    have u considered this:


    does it works for u just fine now? did u tried the first solution?
    no the behavior of the function is same...

  6. #6
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    in ur statechanged function don't execute the code before that:
    xmlHttp.readyState == 4 && xmlHttp.status == 200

    for example put it in a if-condition, only when it's true execute the document. ... etc
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  7. #7
    Join Date
    Jan 2009
    Posts
    4

    Thumbs up

    Quote Originally Posted by Amahdy View Post
    in ur statechanged function don't execute the code before that:
    xmlHttp.readyState == 4 && xmlHttp.status == 200

    for example put it in a if-condition, only when it's true execute the document. ... etc


    thnx ithelped me to solve my problem thxn a lot

Similar Threads

  1. Change page title on AJAX site...JS?
    By raindance in forum AJAX
    Replies: 11
    Last Post: 12-17-2008, 07:27 PM
  2. Replies: 1
    Last Post: 09-11-2008, 07:30 PM
  3. Replies: 0
    Last Post: 09-24-2007, 02:58 AM
  4. problem in setting the title of the page
    By karang in forum ASP.NET
    Replies: 6
    Last Post: 04-30-2007, 05:43 AM
  5. Problem with the component Page Counter of IIS
    By Ch‚ble in forum ASP.NET
    Replies: 1
    Last Post: 06-18-2002, 10:53 AM

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center