SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict jasongr's Avatar
    Join Date
    Jul 2004
    Location
    usa
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to dynamically build parameters to a function

    Hello

    I have the following function:
    Code:
    function preloadImages() {
        a = preloadImages.arguments; 
        for (i=0; i<a.length; i++) {
            Image image = new Image; 
            image.src= a[i];
        }
    }
    I would like to build the parameters that will be sent to the function like so:
    Code:
    var str="";
    str += ' "./Themes/Site/Images/prevPressed.png" ';
    str += ', "./Themes/Site/Images/prev.png" ';
    preloadImages(str);
    The problem is that in function preloadImages, the number of arguments is 1 instead of 2. The arguments should be:
    • "./Themes/Site/Images/prevPressed.png"
    • "./Themes/Site/Images/prev.png"

    Instead I get only of argument:
    • "./Themes/Site/Images/prevPressed.png", "./Themes/Site/Images/prev.png"


    what am I doing wrong?

  2. #2
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can go about this in two ways.

    The first is to split your aguments

    a = preloadImages.arguments.split(",")

    and the second is to use str as an array

    str=new Array
    str[0]="./Themes/Site/Images/prevPressed.png"
    str[1]="./Themes/Site/Images/prev.png"
    preloadImages(str);

    hope it helps
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  3. #3
    SitePoint Addict jasongr's Avatar
    Join Date
    Jul 2004
    Location
    usa
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know that the array solution works because I ended up using it.
    However, I would like to know what was wrong with the first solution.

    Here is what I was trying to do:

    The usage of ',' was in fact to cause the function to think that it is taking multiple arguments. I was in fact trying to send multiple parameters and not just one string separated by ','

  4. #4
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You were adding the strings together, that's why the function only saw 1. You need to seperate them again within the function, as in my first example. Glad you worked it out though
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  5. #5
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How did you get this:

    a = preloadImages.arguments.split(",")

    ...or this:

    str=new Array
    str[0]="./Themes/Site/Images/prevPressed.png"
    str[1]="./Themes/Site/Images/prev.png"
    preloadImages(str);

    ...to work? Neither will work. The problem is: those commas are language operators - not string characters - so they won't 'separate' any arguments fed to the function, but simply be included in them. This does work:

    var a = arguments[0].split(",");

    You can also use:
    Code:
    function preloadImages()
    {
    	var args = (arguments[0].constructor == Array) ? arguments[0] : arguments;		
    	for (var i = 0; i < args.length; ++i)
    		alert(args[i]);
    }
    
    var urls = [
    		'./Themes/Site/Images/prevPressed.png' , 
    		'./Themes/Site/Images/prev.png'
    ];
    
    preloadImages(urls);
    FunctionName.arguments is deprecated.
    ::: certified wild guess :::

  6. #6
    SitePoint Addict jasongr's Avatar
    Join Date
    Jul 2004
    Location
    usa
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the explanation. It was really good.


Bookmarks

Posting Permissions

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