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 4 of 4
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    18
    Thanks
    1
    Thanked 1 Time in 1 Post

    why doesnt stateChanged() take arguments?

    Hi!

    I have made this script, and it works, but I wonder why I cant use arguments in the stateChanged function? Even if I only add brackets in the call for the function, without arguments, it fail. Why? And do you have suggestions on how to avoid making one new function (statechanged2 etc. ) for each content changed?

    Jørgen

    Code:
    var xmlhttp;
    var xmlhttp2;
    
    function changeContent(str)
    {
    if (str.length==0)
      {
       return;
      }
    
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
      {
      alert ("Your browser does not support XMLHTTP!");
      return;
      }
    var url=str;
    url=url+"_xml/";
    url=url+"headerContent.txt";
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    
    
    xmlhttp2=GetXmlHttpObject();
    if (xmlhttp2==null)
      {
      alert ("Your browser does not support XMLHTTP!");
      return;
      }
    
    var url=str;
    url=url+"_xml/";
    url=url+"mainContent.txt";
    xmlhttp2.onreadystatechange=stateChanged2;
    xmlhttp2.open("GET",url,true);
    xmlhttp2.send(null);
    
    
    }
    
    
    function stateChanged()
    {
    if (xmlhttp.readyState==4)
      {
      document.getElementById("headerContent").innerHTML=xmlhttp.responseText;
      }
    }
    
    
    function stateChanged2()
    {
    if (xmlhttp2.readyState==4)
      {
      document.getElementById("mainContent").innerHTML=xmlhttp2.responseText;
      }
    }
    
    
    
    function GetXmlHttpObject()
    {
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
    }

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Well, it does. Since it's an event handler, it takes one (optional) argument, the event object attached to it. Can you explain what you're trying to accomplish?

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by jorgenfi View Post
    And do you have suggestions on how to avoid making one new function (statechanged2 etc. ) for each content changed?
    Jørgen
    something like this?


    Code:
    function fetch(url, div){
     if(div.charAt) { div= document.getElementById(div);}
      var X=!window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest;
      X.open("GET",url,true);
      X.onreadystatechange=function(){
    	if (X.readyState==4){div.innerHTML=X.responseText;}
      }
      X.send(null);
    }
    
    var str=""; //define your path here
    fetch( str+"_xml/headerContent.txt", "headerContent");
    fetch( str+"_xml/mainContent.txt",   "mainContent");
    Last edited by rnd me; 05-15-2009 at 05:40 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • Users who have thanked rnd me for this post:

    jorgenfi (05-16-2009)

  • #4
    New Coder
    Join Date
    May 2009
    Posts
    18
    Thanks
    1
    Thanked 1 Time in 1 Post
    Thank you so much, rnd_me, that inline-function solved my problem!


  •  

    Posting Permissions

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