Web Analytics Made Easy -
StatCounter Global var from within a function - CodingForum

Announcement

Collapse
No announcement yet.

Global var from within a function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Global var from within a function

    I have a function that is passed a variable.

    I need to then make the variable global so it is available to another function

    Code:
    function getspecial([COLOR="Red"]str[/COLOR])
    {
      
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
     {
     alert ("Browser does not support HTTP Request")
     return
     }
    var url="index.php/home/getSpecial/"
    url=url+str
    
    xmlHttp.onreadystatechange=stateChanged 
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
    }
    
    function stateChanged() 
    { 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
     { 
     document.getElementById([COLOR="Red"]str[/COLOR]).innerHTML=xmlHttp.responseText 
     } 
    }
    How do i get str into the next function as shown in red?
    Help much appreciated.

  • #2
    Within the getspecial function you could declare stateChangedStr() = new Function ( "stateChanged(str);" ); and call it like that: xmlHttp.onreadystatechange=stateChangedStr;
    .My new Javascript tutorial site: http://reallifejs.com/
    .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
    .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

    Comment


    • #3
      What am I missing venegal?
      Tx for you idea.
      I am a noobie javascript
      Code:
      function getspecial(str)
      {
      stateChangedStr() = new Function ( "stateChanged(str);" );
      xmlHttp=GetXmlHttpObject()
      if (xmlHttp==null)
       {
       alert ("Browser does not support HTTP Request")
       return
       }
      var url="index.php/home/getSpecial/"
      url=url+str
      
      xmlHttp.onreadystatechange=stateChangedStr; 
      xmlHttp.open("GET",url,true)
      xmlHttp.send(null)
      }
      function stateChanged() 
      { 
      if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
       { 
       document.getElementById('??').innerHTML=xmlHttp.responseText 
       } 
      }

      Comment


      • #4
        you only need to move one char for it to work:

        Code:
        function getspecial(str)
        {
          
        [COLOR="Red"]var [/COLOR]xmlHttp=GetXmlHttpObject()
        if (xmlHttp==null)
         {
         alert ("Browser does not support HTTP Request")
         return
         }
        var url="index.php/home/getSpecial/"
        url=url+str
        
        xmlHttp.onreadystatechange=stateChanged 
        xmlHttp.open("GET",url,true)
        xmlHttp.send(null)
        
        
        function stateChanged() 
        { 
        if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
         { 
         document.getElementById(str).innerHTML=xmlHttp.responseText 
         } 
        }
        
        [COLOR="Red"]}[/COLOR]
        you can then make the xhr object local, which is a probably good idea as well
        Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

        Comment


        • #5
          Sorry, I didn't mention that in my post above:
          stateChangedStr calls stateChanged with the parameter str now, so you will have to make it accept that parameter:

          Code:
          function stateChanged(str)
          { 
          if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
           { 
           document.getElementById(str).innerHTML=xmlHttp.responseText 
           } 
          }
          Last edited by venegal; Apr 16, 2009, 11:27 AM.
          .My new Javascript tutorial site: http://reallifejs.com/
          .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
          .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

          Comment


          • #6
            Originally posted by venegal View Post
            Sorry, I didn't mention that in my post above:
            stateChangedStr calls stateChanged with the parameter str now, so you will have to make it accept that parameter:

            Code:
            function stateChanged(str)
            { 
            if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
             { 
             document.getElementById(str).innerHTML=xmlHttp.responseText 
             } 
            }
            Why, just remove the param so that the stateChanged doesn't have it's own copy, and inherits "str" from getspecial...

            or, dupe it, and allow either:

            Code:
            function stateChanged(str2){
              var str=str2||str;
            Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

            Comment


            • #7
              str is scoped within getspecial, stateChanged will never inherit it, that's what the original problem is all about, and that's why passing str around is even necessary. I've been a bit sloppy before though, here's the revised code:
              Code:
              function getspecial(str){
              	stateChangedStr = function(){
              		stateChanged(str);
              	};
              
              	var xmlHttp=GetXmlHttpObject()
              	if (xmlHttp==null){
              		alert ("Browser does not support HTTP Request");
              		return;
              	}
              	
              	var url = "index.php/home/getSpecial/";
              	url = url + str;
              	
              	xmlHttp.onreadystatechange = stateChangedStr;
              	xmlHttp.open("GET",url,true);
              	xmlHttp.send(null);
              }
              function stateChanged(str){ 
              	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete"){ 
              		document.getElementById(str).innerHTML = xmlHttp.responseText;
              	} 
              }
              If you're feeling comfortable polluting your global namespace, you could of course always just declare var globalStr; outside of any function, set it in getspecial to globalStr = str;, use it in statechanged document.getElementById(globalStr).innerHTML=xmlHttp.responseText and apart from that use your original code. I guess that's what you originally had in mind.
              .My new Javascript tutorial site: http://reallifejs.com/
              .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
              .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

              Comment


              • #8
                So this this works but.. for some reason which is in another post it won't send the variables in the post.



                Code:
                function getspecial(str)
                {
                  
                xmlHttp=GetXmlHttpObject()
                if (xmlHttp==null)
                 {
                 alert ("Browser does not support HTTP Request")
                 return
                 }
                var url="index.php/home/getSpecial/"
                url=url+str
                
                xmlHttp.onreadystatechange=stateChanged 
                xmlHttp.open("GET",url,true)
                xmlHttp.send(null)
                var div = str.substr(6,1);
                function stateChanged() 
                { 
                if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
                 { 
                 document.getElementById(div).innerHTML=xmlHttp.responseText 
                 } 
                }
                }
                Here is the relevant post:
                http://codingforum.net/showthread.php?t=164069

                Comment

                Working...
                X