views:

334

answers:

1

HI i have flowing code that runs as expected in Firefox and in IE6 it runs proper at first time and when the second call made it makes problem and returns old value

function loadCartItems()
{
var xmlhttp23;

if(xmlhttp23!= 'undefined')
{
xmlhttp23=getXmlHttpObject();
}

xmlhttp23.onreadystatechange=function()
{

if(xmlhttp23.readyState==4)
{

alert(xmlhttp23.responseText);
}
}
xmlhttp23.open("GET","../printerink/ItemsInCart.aspx",true);
xmlhttp23.send(null);
xmlhttp23=null;

}

function getXmlHttpObject()
{
    var request = null;

    /* Does this browser support the XMLHttpRequest object? */
    if (window.XMLHttpRequest) {
        if (typeof XMLHttpRequest != 'undefined')
            /* Try to create a new XMLHttpRequest object */
            try {
                request = new XMLHttpRequest( );
            } catch (e) {
                request = null;
            }
    /* Does this browser support ActiveX objects? */
    } else if (window.ActiveXObject) {
        /* Try to create a new ActiveX XMLHTTP object */
        try {
            request = new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e) {
            try {
                request = new ActiveXObject('Microsoft.XMLHTTP');
            } catch (e) {
                request = null;
            }
        }
    }
    return request;
}

here i am going to alert the result i have checked at every place by using alert every thing is working proper but i just noticed one problem that is as i am using aspx page to return result in this aspx page i set the break points for debug and found that when first time page get loads these break points fire and if the page loading 2nd time from same IE6 window they are not fired and one thing more that is XMLHTTP all things are working like xmlhttp.readyState is 4 and xmlhttp.status is 200 and just only the xmlhttp.open seems that it is not executing

plz help

+1  A: 

Frankly I am confused by your code. I think some clarification is needed before it is possible to help. First off, what is the intention of this:

function loadCartItems() {
    var xmlhttp23;
    if(xmlhttp23!= 'undefined') {
        xmlhttp23=getXmlHttpObject();
    }
    ...
    xmlhttp23=null;
}

I mean, by definition, the local variable xmlhttp23 will always be undefined whenever you enter loadCartItems(). Then you test xmlhttp23!= 'undefined' but this doesn't really make sense: xmlhttp23 will never be equal to the string literal 'undefined'.

I don't understand the last line xmlhttp23=null either: is it your intention to explicitly clean up the XMLHttpRequest object? It seems to me this isn't really necessary, because the local variable xmlhttp23 will be out of scope anyway after the loadCartItems() function finishes.

Looking at the initialization code for xmlhttp23, it almost looks like you intended to create a XMLHttpRequest just once, and want to reuse that. If that is the case, I think your code should be:

var xmlhttp23;
function loadCartItems() {       
    if(!xmlhttp23) {
        xmlhttp23 = getXmlHttpObject();
    }
    xmlhttp23.open("GET","../printerink/ItemsInCart.aspx",true);
    xmlhttp23.onreadystatechange = function() {
        if (xmlhttp23.readyState==4) {
            if (xmlhttp23.status==200) { //success
                alert(xmlhttp23.responseText);
            }
            else { //error
                alert("Whoops: " + xmlhttp23.statusText);
            }
        }
    }
    xmlhttp23.send(null);
}

Note that the onreadystatechange handler must be assigned after calling the open() method. If you don't, you can't reuse the Xhr object in IE6.

Read more about why that is here:

http://keelypavan.blogspot.com/2006/03/reusing-xmlhttprequest-object-in-ie.html

Roland Bouman