passing parameter of a function to array

Discussion in 'Javascript' started by cuco, Feb 22, 2011.

  1. cuco

    cuco Guest

    I want to use the parameter of a function as a variable for an array.

    Example:
    <html>
    <head>
    my function (parameter){
    x=new Array(parameter);
    // rest of the code using x
    }
    </head>
    <body>
    <script language="javascript">my function(" ' item1' , ' item2' , '
    item3' ")</script>
    </body>
    </html>

    It's not working.
    cuco, Feb 22, 2011
    #1
    1. Advertising

  2. cuco wrote:

    > I want to use the parameter of a function as a variable for an array.


    You probably want to use the passed arguments as arguments of Array
    constructor? Your explanation and the posted code are not clear. The
    code is not syntactical correct and I think you should learn the
    lexical grammar of the language in first place. Good starting point
    is:
    <URL:https://developer.mozilla.org/en/JavaScript/Guide>

    On entering in new execution context, the interpreter automatically
    creates array like object and binds with `arguments' property of
    variable object associated with that execution context. In other
    words, if you declare the follow function.

    function F() {
    return arguments.length;
    }

    F(1, 2, 3, 4);

    It will return the number of passed arguments. If you want to access
    the value of the second argument for example, you should write:

    arguments[1]; //2

    Some interpreters do not create arguments object from performance
    purposes. They only create it, if the `arguments' is used in the body
    of the function.

    Also `arguments' object is not truly array instance object. At least
    it has not got reference to `Array.prototype' in its prototype chain.
    So you could not use:

    arguments.push(10, 20, 30); //TypeError

    If you would like to "convert" arguments object to truly array
    instance.

    var arr = Array.apply([], arguments);
    Asen Bozhilov, Feb 22, 2011
    #2
    1. Advertising

  3. cuco

    cuco Guest

    Sorry, I was too much in a hurry. This is the complete code. I try to
    change the background image of a div, like a kind of slideshow. I want
    to put the script in the head on an extern js file, so I can use it on
    every page. The images are different on every page.

    If you activate the alert, you get the correct arguments for the
    array. I'm not an expert, I think this should work, but it doesn't.
    The error console of Firefox says: "parameter is not defined."


    All images are jpg files, all images are in the same folder images.


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
    www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html;
    charset=iso-8859-1" />
    <title>Rotate Background Image</title>
    <script language="JavaScript" type="text/javascript">
    function rotateBackgroundImage(parameter){
    imagePaths=new Array(parameter);
    //alert(imagePaths);
    thisPath=0;
    backgroundImageCt=imagePaths.length;
    thisPath++
    if(thisPath==backgroundImageCt){thisPath=0;}
    document.getElementById("myDiv").style.backgroundImage="url(images/"+imagePaths[thisPath]
    +".jpg)";
    document.getElementById('myDiv').style.backgroundRepeat="no-repeat";
    document.getElementById('myDiv').style.width="600px";
    document.getElementById('myDiv').style.height="400px";
    document.getElementById('myDiv').style.position="absolute";
    document.getElementById('myDiv').style.top="10px";
    document.getElementById('myDiv').style.left="10px";
    setTimeout("rotateBackgroundImage(parameter)",4000);
    }
    </script>
    </head>
    <body>
    <div id="myDiv"></div>
    <script language="javascript" type="text/
    javascript">rotateBackgroundImage(['image_1','image_2','image_3','image_4'])</
    script>
    </body>
    </html>
    cuco, Feb 24, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nitinm
    Replies:
    2
    Views:
    319
    Barry Schwarz
    Sep 14, 2007
  2. Mister B
    Replies:
    8
    Views:
    522
    Nick Keighley
    Aug 26, 2010
  3. Kai-Uwe Bux
    Replies:
    2
    Views:
    311
    Luc Danton
    Oct 3, 2010
  4. Luc Danton
    Replies:
    2
    Views:
    721
    Luc Danton
    Oct 4, 2010
  5. AzamSharp
    Replies:
    2
    Views:
    152
Loading...

Share This Page