+ Reply to Thread
Results 1 to 5 of 5

Thread: Passing JS variables from one function to another

  1. #1
    Tenant is offline x10Hosting Member Tenant is an unknown quantity at this point
    Join Date
    Jan 2009
    Posts
    13

    Passing JS variables from one function to another

    If i see one person say place the variable outside the functions I am going to slap them. I just read through many forums were that is all they said. I am using Ajax to pull data meaning the data is pulled into a function. I can not place the variables outside it. This is what I need to be able to do.

    Code:
    function1()
    {
     var time = resp.test[0].time;
     var amount = resp.test[0].amount;
     alert(amount);
    
    }
    
    function2(obj)
    {
      if (time > 10){
          doument.test.value = amount
     }
    }

  2. #2
    quantum1's Avatar
    quantum1 is offline x10Hosting Member quantum1 is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    near Nashville, TN
    Posts
    68

    Re: Passing JS variables from one function to another

    At the risk of being slapped I will hazard a reply. :O
    Question: You want to set the variable "time" in function1 and have the value of that variable available in function2?
    Two rules of development:
    1) Computers work for people; People do not work for computers
    2) Maintainability is all that matters.

  3. #3
    Spasm is offline x10Hosting Member Spasm is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    10

    Re: Passing JS variables from one function to another

    Ummm... There isn't any other way to do it besides putting the variable outside the function. :nuts:


    Variables declared inside of a function will die when the function exits. These are called local variables, and can only be accessed from inside of that particular function. Local variables can have the same name as each other, but not the same as global variables that already exist.

    Variables declared outside all functions in the body of the script will survive from the point they are declared until the script ends. These are called global variables, and can be accessed from inside functions as well as outside.


    Code:
    var time = 0;
    
    function1()
    {
     time = resp.test[0].time;
     var amount = resp.test[0].amount;
     alert(amount);
    }
    
    function2(obj)
    {
     if (time > 10)
     {
      document.test.value = amount
     }
    }


    I haven't done any web developing in a while, and I didn't use alot of AJAX when I did, so I may be wrong. I don't understand what you mean by data is being pulled into a function, and you can't declare variables outside of it. This is what should happen:

    1. You declare the global variables
    2. You call your function
    3. Your function sets the global variables
    4. Other function can use the global variables later


    Type "javascript variable scope" into Google for more info.


    PS - There was a typo in function2, "document" was missing the "c". Maybe that was causing problems for you.
    Last edited by Spasm; 02-24-2009 at 10:51 AM.

  4. #4
    quantum1's Avatar
    quantum1 is offline x10Hosting Member quantum1 is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    near Nashville, TN
    Posts
    68

    Re: Passing JS variables from one function to another

    another way to pass information back and forth is to store it in session variables....this might work if the function interactions aren't buzzing along at one million calls per second...which with ajax they might
    Two rules of development:
    1) Computers work for people; People do not work for computers
    2) Maintainability is all that matters.

  5. #5
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Passing JS variables from one function to another

    can you create an object with two methods and the 'time' as a property?

    http://www.w3schools.com/JS/js_objects.asp
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

+ Reply to Thread

Similar Threads

  1. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  2. Replies: 2
    Last Post: 08-17-2008, 08:30 PM
  3. Drop-Down Menu problems
    By gmshed in forum Programming Help
    Replies: 3
    Last Post: 04-11-2008, 12:33 AM
  4. Easy XHTML form validation using PHP
    By Xemnas in forum Tutorials
    Replies: 0
    Last Post: 01-08-2008, 04:29 AM
  5. Passing variables from page to page
    By os242 in forum Scripts & 3rd Party Apps
    Replies: 3
    Last Post: 09-15-2007, 02:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers