SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    XmlHttp + MSIE--is it a reusable object

    So, I have a little module for my apps called "Heartbeat." Its main purpose in life is to provide a heartbeat in order to maintain authentication & session state in admin tools.

    Anyhow, I have had some reports that it was failing in IE. I fixed the first problem--apparently one has to return some sort of content to a XmlHttp request or IE's javascript dies.

    But it appears that there is a deeper issue. It just fires once then dies (or at least never fires again), which kind of sucks because I would like for it to keep going. Before I start significant retooling. Now, it should be reusable from what I understand, as I am never setting the onreadystatechange callback, as I don't care about the data that is returned. Then again that might be the problem.

    But I suck at javascript (thank god for Atlas) so I am probably doing something wrong.

    Script in question is below:

    Code:
    var hb_handlerlocation='_HANDLER_';
    var hb_length=_LENGTH_;
    var hb_pageId='';
    
    function doPingBack()
    {
    	var xmlhttp=getXmlHttp();
    	if (xmlhttp)
    	{
    		xmlhttp.open("GET", getHeartbeatUrl(), true);
    		xmlhttp.send(null);
    	}
    }
    
    function getHeartbeatUrl()
    {
        if (hb_pageId=='')
        {
           return hb_handlerlocation;
        }
        else
        {
           return hb_handlerlocation + '?pageId=' + hb_pageId;
        }
    }
    
    function getXmlHttp()
    {
        http_request=false;
        if (window.XMLHttpRequest)
        {
            http_request = new XMLHttpRequest();
        } 
        else if (window.ActiveXObject) 
        {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
        return http_request;
    }
    
    function setupHeartbeat(pageId)
    {
        hb_pageId=pageId;
        setInterval('doPingBack()', hb_length);
    }
    It is called by running the setupHeartbeat() function. The _SOMETHING_ stuff is merged in from configuration, and can be presumed to be valid/correct.

    I should also note that this functiosn perfectly in firefox.

    Any advice?

  2. #2
    SitePoint Zealot yacka's Avatar
    Join Date
    Aug 2006
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It looks like it's creating a new XmlHttpRequest object for every 'ping' anyway, so I don't think reusability is your issue. Try defining http_request in the global scope and checking if it is null before initialisation in GetXmlHttp. Also, it seems stupid to me that GetXmlHttp either returns an object or a bool. Just have it return an object or null and check xmlhttp for null in the doPingBack function.

    Not saying this is going to solve your problem, but more information helps.

  3. #3
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Good point on the reusability thing. I'll see about putting it in the global scope and fixing the false/null stuff.

    Anyone else?

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    IE assumes that multiple GET requests passing exactly the same URL and querystring are going to return the same information and so it returns the cached data returned from the first call rather than actually calling the server again.

    There are two ways to fix this:
    1. Add a no-cache header to the data being returned from the server so that IE doesn't cache it in the first place
    2. Add the current time as a dummy parameter in the query string so as to make each call different.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Thanks Stephen, that did the trick it seems.

  6. #6
    SitePoint Enthusiast GzThai's Avatar
    Join Date
    Sep 2006
    Location
    USA
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Stephen, good job


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
  •