Results 1 to 6 of 6

Thread: [RESOLVED] Use parameter of function as return?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Resolved [RESOLVED] Use parameter of function as return?

    How would I do something like this:

    Code:
    var theVariable = "";
    var wasSuccess = false;
    
    function doSomething(theParameter)
    {
    
      theParameter = "I'm the parameter!";
    
      return true;
    
    }
    
    wasSuccess = doSomething(theVariable);
    
    alert(theVariable);
    And it will alert "I'm the parameter!".

    Basically I need to change the variable's value that's specified as a parameter. Not sure if it's possible to do with JavaScript, but I'm sure I've seen it done before.

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Use parameter of function as return?

    You'll want to pass the parameter by reference.

    http://php.net/manual/en/language.references.pass.php

    *edit* Whoops... Thought I was in the PHP section. Take a look at this instead: http://snook.ca/archives/javascript/javascript_pass/
    Last edited by kfcSmitty; May 22nd, 2012 at 08:09 AM.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Use parameter of function as return?

    well, if it's displaying "I'm the parameter" in the alert box.... doesn't that answer your question?
    Or are you asking if that will work? If that's the case, then it should have taken you all of 5 minutes to test that out. create an HTML file, add the script, open it and see what happens.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Use parameter of function as return?

    Call-time pass-by-reference is a headache. JavaScript doesn't support it. Instead, you could pass an object containing a property, and modify the value of that property.

    But your example doesn't make a lot of sense, because the new value is known before the function call, and so the function call is redundant. Show us what you really need to do and we'll come up with a way to do it.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Use parameter of function as return?

    kfcSmitty got it spot on! That's exactly what I was trying to achieve.

    Basically the reason I'm doing this is I've got a JS function that I use for AJAX. It's all fine when I'm updating the webpage, but when I want to update a JS variable, it's a different story.

    Here's what I mean:

    PHP Code:
    var httpReqs = new Array;

    function 
    getFromServer(queryAndURLidToUpdate)
    {
      var 
    newUpper 0;
      
    httpReqs[httpReqs.length+1]=false;
      
    newUpper=httpReqs.length;
      
      if(
    navigator.appName == "Microsoft Internet Explorer")
      {
        
    httpReqs[newUpper] = new ActiveXObject("Microsoft.XMLHTTP");
      }
      else
      {
        
    httpReqs[newUpper] = new XMLHttpRequest();
      }
      
      
    httpReqs[newUpper].open("GET"queryAndURLtrue);
      
    httpReqs[newUpper].onreadystatechange=function()
      {
        if(
    httpReqs[newUpper].readyState == 4)
        {
          
    document.getElementById(idToUpdate).innerHTML httpReqs[newUpper].responseText;
        }
      }
      
    httpReqs[newUpper].send(null);
      
      return 
    0;
      

    I will be modifying this function so that when called, it updates what ever variable that's passed as a parameter.

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] Use parameter of function as return?

    Why don't you just pass the event handler in?

    Code:
    function getFromServer(queryAndURL, callback)
    {
      // ...
      httpReqs[newUpper].onreadystatechange = callback;
      // ...
    }

Posting Permissions

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



Click Here to Expand Forum to Full Width