XMLHttpRequest IE vs FireFox

Discussion in 'Javascript' started by shankwheat, Jun 1, 2007.

  1. shankwheat

    shankwheat Guest

    I use this code to populate a selectbox with a group of records from a
    database. It executes very quickly with FireFox 2.0 but takes 7-10
    secs with IE6 and IE7. Just wondering if anyone had any pointers for
    improving this code to make IE run it faster or I am just stuck with
    it? Thanks

    function GetXmlHttpObject()
    {
    var objXMLHttp=null

    try {
    objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP"); //later IE
    } catch (e) {
    try {
    objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); //earlier IE
    } catch (e) {
    objXMLHttp = null;
    }
    }

    if (objXMLHttp==null)
    {
    objXMLHttp=new XMLHttpRequest() //IE7, Firefox, Safari
    }
    return objXMLHttp
    }


    function getCompanies()
    {
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
    alert ("Your browser does not support the XMLHttpRequest object.")
    return
    }

    var url="getCompanies.aspx"
    xmlHttp.onreadystatechange=stateChanged
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
    document.getElementById("lblResults").innerHTML = "<img
    src='mozilla_blu.gif' alt='loading..please wait'>"; //loading
    }


    function stateChanged()
    {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
    document.getElementById("lblResults").innerHTML = "";

    // Clear the available listbox of any previous options
    document.choiceForm.available.length = 0;

    // Split the delimited response into an array
    var s = xmlHttp.responseText;
    var re = new RegExp('{([^{}]*)}','g');
    var x = [];
    while (re.exec(s)){
    x.push(RegExp.$1.split('#'));
    }

    // Add the array options to the selectbox available
    var sel = document.forms['choiceForm'].elements['available'];
    for (var i=0, len=x.length; i<len; i++){
    sel.options = new Option(x[1], x[0]);
    }
    }
    }
     
    shankwheat, Jun 1, 2007
    #1
    1. Advertising

  2. shankwheat wrote:
    > I use this code to populate a selectbox with a group of records from a
    > database. It executes very quickly with FireFox 2.0 but takes 7-10
    > secs with IE6 and IE7. Just wondering if anyone had any pointers for
    > improving this code to make IE run it faster or I am just stuck with
    > it? Thanks
    >
    > function GetXmlHttpObject()
    > {
    > var objXMLHttp=null
    >
    > try {
    > objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP"); //later IE
    > } catch (e) {
    > try {
    > objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); //earlier IE
    > } catch (e) {
    > objXMLHttp = null;
    > }
    > }
    >
    > if (objXMLHttp==null)
    > {
    > objXMLHttp=new XMLHttpRequest() //IE7, Firefox, Safari


    Unless an IE 7 user has scripting of ActiveX objects disabled your code
    will never end up here with IE 7. Thus if you want your code to use
    XMLHttpRequest with IE 7 you should check
    if (typeof XMLHttpRequest != 'undefined') {
    objXMLHttp = new XMLHttpRequest();
    }
    first, then if that fails you should use the new ActiveXObject code.



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jun 2, 2007
    #2
    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. David
    Replies:
    10
    Views:
    388
    Daniel Kabs
    May 4, 2005
  2. gobera

    XMLHttpRequest and Firefox Problem

    gobera, May 3, 2005, in forum: Javascript
    Replies:
    0
    Views:
    81
    gobera
    May 3, 2005
  3. Rich Hephner

    XMLHttpRequest, Firefox and IE inconsistent

    Rich Hephner, May 9, 2005, in forum: Javascript
    Replies:
    4
    Views:
    107
    Rich Hephner
    May 10, 2005
  4. Sergey Surkov

    XMLHttpRequest in FireFox and browser cache

    Sergey Surkov, Jun 4, 2005, in forum: Javascript
    Replies:
    3
    Views:
    147
    Sergey Surkov
    Jun 4, 2005
  5. warteschlange

    XMLHttpRequest in FireFox and cache (Part 2)

    warteschlange, Aug 7, 2005, in forum: Javascript
    Replies:
    2
    Views:
    106
    Martin Honnen
    Aug 7, 2005
Loading...

Share This Page