Script working in Firefox but not IE7

Discussion in 'Javascript' started by TheDude5B, Sep 10, 2007.

  1. TheDude5B

    TheDude5B Guest

    Hi, I have this piece of AJAX Javascript which I copied from the
    w3schools.com website, which is supposed to work with most browser,
    yet it doesnt work with IE7.

    <script type="text/javascript">
    var xmlHttp

    function countCartItems()
    {
    //if (str.length==0)
    //{
    //document.getElementById("cartcountfield").innerHTML="";
    //return;
    //}
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
    alert ("Your browser does not support AJAX!");
    return;
    }
    var url="getCartCount.aspx";
    //url=url+"?q="+str;
    //url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    }

    function stateChanged()
    {
    if (xmlHttp.readyState==4)
    {

    document.getElementById("cartcountfield").innerHTML=xmlHttp.responseText;
    }
    }

    function GetXmlHttpObject()
    {
    var xmlHttp=null;
    try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
    // Internet Explorer
    try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    }
    return xmlHttp;
    }
    </script>


    Can someone help me out here to why this is not working with IE7?

    Thanks
     
    TheDude5B, Sep 10, 2007
    #1
    1. Advertising

  2. TheDude5B

    Erwin Moller Guest

    TheDude5B wrote:
    > Hi, I have this piece of AJAX Javascript which I copied from the
    > w3schools.com website, which is supposed to work with most browser,
    > yet it doesnt work with IE7.


    Hi,

    A quick pointer: the GetXmlHttpObject() DOES work in IE7.
    At least on my Vista Home Premium IE7.

    This looks allright too:
    document.getElementById("cartcountfield").innerHTML=xmlHttp.responseText;

    Could you elaborate WHAT is not working in your situation?

    Does your counter not get invoked?
    Do you get some error?

    Maybe add an alert to stateChanged() function to see if it gets called
    at all?

    Did you try to debug by placing a few strategically placed alerts?

    Regards,
    Erwin Moller


    >
    > <script type="text/javascript">
    > var xmlHttp
    >
    > function countCartItems()
    > {
    > //if (str.length==0)
    > //{
    > //document.getElementById("cartcountfield").innerHTML="";
    > //return;
    > //}
    > xmlHttp=GetXmlHttpObject()
    > if (xmlHttp==null)
    > {
    > alert ("Your browser does not support AJAX!");
    > return;
    > }
    > var url="getCartCount.aspx";
    > //url=url+"?q="+str;
    > //url=url+"&sid="+Math.random();
    > xmlHttp.onreadystatechange=stateChanged;
    > xmlHttp.open("GET",url,true);
    > xmlHttp.send(null);
    > }
    >
    > function stateChanged()
    > {
    > if (xmlHttp.readyState==4)
    > {
    >
    > document.getElementById("cartcountfield").innerHTML=xmlHttp.responseText;
    > }
    > }
    >
    > function GetXmlHttpObject()
    > {
    > var xmlHttp=null;
    > try
    > {
    > // Firefox, Opera 8.0+, Safari
    > xmlHttp=new XMLHttpRequest();
    > }
    > catch (e)
    > {
    > // Internet Explorer
    > try
    > {
    > xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    > }
    > catch (e)
    > {
    > xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    > }
    > }
    > return xmlHttp;
    > }
    > </script>
    >
    >
    > Can someone help me out here to why this is not working with IE7?
    >
    > Thanks
    >
     
    Erwin Moller, Sep 10, 2007
    #2
    1. Advertising

  3. TheDude5B

    TheDude5B Guest

    On Sep 10, 2:38 pm, Erwin Moller
    <> wrote:
    > TheDude5B wrote:
    > > Hi, I have this piece of AJAX Javascript which I copied from the
    > > w3schools.com website, which is supposed to work with most browser,
    > > yet it doesnt work with IE7.

    >
    > Hi,
    >
    > A quick pointer: the GetXmlHttpObject() DOES work in IE7.
    > At least on my Vista Home Premium IE7.
    >
    > This looks allright too:
    > document.getElementById("cartcountfield").innerHTML=xmlHttp.responseText;
    >
    > Could you elaborate WHAT is not working in your situation?
    >
    > Does your counter not get invoked?
    > Do you get some error?
    >
    > Maybe add an alert to stateChanged() function to see if it gets called
    > at all?
    >
    > Did you try to debug by placing a few strategically placed alerts?
    >
    > Regards,
    > Erwin Moller
    >
    >
    >
    >
    >
    > > <script type="text/javascript">
    > > var xmlHttp

    >
    > > function countCartItems()
    > > {
    > > //if (str.length==0)
    > > //{
    > > //document.getElementById("cartcountfield").innerHTML="";
    > > //return;
    > > //}
    > > xmlHttp=GetXmlHttpObject()
    > > if (xmlHttp==null)
    > > {
    > > alert ("Your browser does not support AJAX!");
    > > return;
    > > }
    > > var url="getCartCount.aspx";
    > > //url=url+"?q="+str;
    > > //url=url+"&sid="+Math.random();
    > > xmlHttp.onreadystatechange=stateChanged;
    > > xmlHttp.open("GET",url,true);
    > > xmlHttp.send(null);
    > > }

    >
    > > function stateChanged()
    > > {
    > > if (xmlHttp.readyState==4)
    > > {

    >
    > > document.getElementById("cartcountfield").innerHTML=xmlHttp.responseText;
    > > }
    > > }

    >
    > > function GetXmlHttpObject()
    > > {
    > > var xmlHttp=null;
    > > try
    > > {
    > > // Firefox, Opera 8.0+, Safari
    > > xmlHttp=new XMLHttpRequest();
    > > }
    > > catch (e)
    > > {
    > > // Internet Explorer
    > > try
    > > {
    > > xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    > > }
    > > catch (e)
    > > {
    > > xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    > > }
    > > }
    > > return xmlHttp;
    > > }
    > > </script>

    >
    > > Can someone help me out here to why this is not working with IE7?

    >
    > > Thanks- Hide quoted text -

    >
    > - Show quoted text -


    When running in IE7, I have but alerts in which appear at each
    important stage. An alert fires when it trys to set the xmlHttp=new
    XMLHttpRequest(); (for Firefox and other browsers), but does not then
    continue to the catch statement which is where it should go. It then
    does not fire the alert after this code:
    if (xmlHttp==null)
    {
    alert ("Your browser does not support AJAX!");
    return;
    }

    Also, while debugging my application (it is an asp.net app) the code
    never runs into the var url="getCartCount.aspx" page when in IE7, but
    does in Firefox.
    This is very strange as I remember using this same bit of code a while
    back and it all seemed to work.

    Is there another way which I could do the same thing?
     
    TheDude5B, Sep 11, 2007
    #3
  4. TheDude5B

    TheDude5B Guest

    It seems that this code does work with standard HTML and asp, but does
    not work on IE7 with .aspx files?
     
    TheDude5B, Sep 11, 2007
    #4
  5. TheDude5B

    Erwin Moller Guest

    TheDude5B wrote:
    > It seems that this code does work with standard HTML and asp, but does
    > not work on IE7 with .aspx files?
    >


    Hi Dude,

    I don't know the first thing about .NET, so I cannot help there.

    But when debugging javascript, there is 1 simple rule: simply look at
    the source the server delivers to the browser.
    That is all the browser cares about, NOT how/by what it is generated.

    A tip: since you use FF already, get the following add-on in:
    HTML Validator (version 0.8.4.0 is the latest I think)

    It works of course only in FF, but this tool DID save many hours of
    mindless debugging because it tells you when you view source WHAT is
    illegal code.
    Great place to start. Even though you do not have problems in FF.

    I found stupid things that are hard to find, like PHP-code coming from a
    MAC that used bad END-OF-LINE, that didn't show up in my editor.

    Chances are you'll quickly find strange stuff in your code if you use
    this add-on.

    Give it a shot. Good luck.

    Regards,
    Erwin Moller
     
    Erwin Moller, Sep 11, 2007
    #5
  6. "Erwin Moller"
    <> wrote in
    message news:46e65b38$0$227$4all.nl...
    > TheDude5B wrote:
    >> It seems that this code does work with standard HTML and asp, but does
    >> not work on IE7 with .aspx files?
    >>

    >
    > Hi Dude,
    >
    > I don't know the first thing about .NET, so I cannot help there.
    >
    > But when debugging javascript, there is 1 simple rule: simply look at the
    > source the server delivers to the browser.
    > That is all the browser cares about, NOT how/by what it is generated.
    >
    > A tip: since you use FF already, get the following add-on in:
    > HTML Validator (version 0.8.4.0 is the latest I think)
    >
    > It works of course only in FF, but this tool DID save many hours of
    > mindless debugging because it tells you when you view source WHAT is
    > illegal code.
    > Great place to start. Even though you do not have problems in FF.
    >
    > I found stupid things that are hard to find, like PHP-code coming from a
    > MAC that used bad END-OF-LINE, that didn't show up in my editor.
    >
    > Chances are you'll quickly find strange stuff in your code if you use
    > this add-on.
    >
    > Give it a shot. Good luck.
    >


    I would also highly recommend Firebug (www.getfirebug.com), where you can
    set breakpoints and use mouseover with the cursor to examine values of
    variables. It was suggested to me in my newbie post on the microsoft
    Jscript NG.

    Paul
     
    Paul E. Schoen, Sep 11, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aidan
    Replies:
    4
    Views:
    506
    Neredbojias
    Jun 7, 2007
  2. OccasionalFlyer

    Page shows in FIrefox, IE7 but not IE6

    OccasionalFlyer, Dec 4, 2008, in forum: HTML
    Replies:
    4
    Views:
    445
    Chaddy2222
    Dec 5, 2008
  3. APA
    Replies:
    0
    Views:
    1,272
  4. garey
    Replies:
    1
    Views:
    122
    Thomas 'PointedEars' Lahn
    Apr 30, 2009
  5. garey
    Replies:
    19
    Views:
    236
    David Mark
    May 7, 2009
Loading...

Share This Page