Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    XMLHttp Mozilla Awkwardness

    Hi,

    I am using XMLHttp to load data from a database without a
    page.
    It is working perfect in Internet Explorer, but it is giving me
    troubles in Mozilla Firefox.

    This is the code in which XMLHttp is initialized and the function
    which is used to load a page:


    Code:
    var xmlhttp=false; 
    /*@cc_on @*/ 
    /*@if (@_jscript_version >= 5) 
    // JScript gives us Conditional compilation, we can cope with old IE versions. 
    // and security blocked creation of the objects. 
      try { 
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
      } catch (e) { 
       try { 
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
       } catch (E) { 
        xmlhttp = false; 
       } 
      } 
    @end @*/ 
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') { 
      xmlhttp = new XMLHttpRequest(); 
    } 
    
    function loadFragmentInToElement(fragment_url, element_id) { 
        var element = document.getElementById(element_id); 
    
        xmlhttp.open("GET", fragment_url); 
        xmlhttp.onreadystatechange = function() { 
          if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
    
          element.innerHTML = xmlhttp.responseText; 
          } 
        } 
        xmlhttp.send(null); 
    }
    I now call loadFragmentInToElement in an onClick event handler of
    a link, as follows:


    Code:
    function showContact() { 
            loadFragmentInToElement(baseurl + 'get_contact.php', 'content'); 
          }

    In this baseurl is a variabele which is set to the path of get_contact.php. Furthermore on the page a div exists with id = 'content', in which the result of get_contact.php should be loaded.
    Now it is the case that in IE indeed get_contact.php is loaded into this
    div, but in Mozilla nothing happens.

    Now the strangeness begins:
    When I add some 'alert' in showContact, just before calling loadFragmentInToElement, the alert shows up and afterwards get_contact.php is loaded into the div, also in Firefox.

    So by adding something like: alert('hi'); in front of the call to the function
    it suddenly works in Mozilla.

    Does anybody know what causes this and, even better, a solution for this problem?

    Regards David

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Try specifying true to make an asynchronous request.
    Code:
    xmlhttp.open("GET", fragment_url, true);
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This did not make a difference unfortionately.
    No extra information could be retrieved after adding the true which could be relevant to this topic.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    What extra information?
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That remark was added to stress the fact that it made not difference and thus there is no extra information (like error output or something like that) that I can add to my post after trying your suggestion.

  • #6
    Regular Coder
    Join Date
    Aug 2005
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    works for me!
    Code:
    <html>
    <head>
    <script>
      var xmlhttp;
      try{xmlhttp = new XMLHttpRequest();}catch(e){
      try{xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");}catch(e){
      try{xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}}}
      function go() {
        if(!xmlhttp)return alert("No Http Transport.");
        xmlhttp.open("GET",document.location.href,true);
        xmlhttp.onreadystatechange = function() {
          if (xmlhttp.readyState == 4) {
            document.getElementById("output").innerHTML = 
              "<textarea rows=\"20\" cols=\"60\">" + 
              xmlhttp.responseText + "</textarea>";
          }
        }
        xmlhttp.send(null);
      }
    </script>
    </head>
    <body>
      <button onclick="go()">Go</button>
      <div id="output"></div>
    </body>
    </html>

  • #7
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It still does not work for me, at least not al the time.

    This is the output of the javascript console

    Error: uncaught exception: [Exception... "Component returned failure code: 0xc1f30001 (NS_ERROR_NOT_INITIALIZED) [nsIXMLHttpRequest.send]" nsresult: "0xc1f30001 (NS_ERROR_NOT_INITIALIZED)" location: "JS frame :: http://localhost/heco/js/xmlhtml.js :: loadFragmentInToElement :: line 17" data: no]

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Try initializing xmlhttp everytime loadFragmentInToElement() is called.
    Code:
    var xmlhttp; 
    
    function loadFragmentInToElement(fragment_url, element_id) { 
        var element = document.getElementById(element_id); 
    
      /*@cc_on @*/ 
      /*@if (@_jscript_version >= 5) 
      // JScript gives us Conditional compilation, we can cope with old IE versions. 
      // and security blocked creation of the objects. 
      try { 
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
      } catch (e) { 
        try { 
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
        } catch (E) { 
          xmlhttp = false; 
        } 
      } 
    @end @*/ 
    
      if (!xmlhttp && typeof XMLHttpRequest!='undefined') { 
        xmlhttp = new XMLHttpRequest(); 
      } 
    
        xmlhttp.open("GET", fragment_url); 
        xmlhttp.onreadystatechange = function() { 
          if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
    
          element.innerHTML = xmlhttp.responseText; 
          } 
        } 
        xmlhttp.send(null); 
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •