Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    525
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dynamically creating variables.

    I have this code:

    Code:
    <script>
    
    
    function createVar() {
    for(i=1;i<document.links.length+1;i++){
    var joe=new Array();
    joe[i]=0;
    }
    }
    
    </script>
    
    <body onload="createVar();">
    
    <a href=""> </a>
    <a href=""> </a>
    <a href=""> </a>
    <a href=""> </a>
    <a href=""> </a>
    <a href=""> </a>
    <a href=""> </a>
    <a href=""> </a>
    <a href=""> </a>
    <a href=""> </a>
    <a href=""> </a>
    <a href=""> </a>
    <a href=""> </a>
    <a href=""> </a><br>
    that is supposed to make a variable each time it sees a link, I have no idea if it is working or not but whenever I call to a function with the variable "joe" (ex. joe[1]), it says joe is undefined. I am making this for a fading script I made so that none of the fades clash together, this is my fading script:

    Code:
    <script>
    
    var i=0;
    var j=0;
    var k=0; 
    var Timer;
    
    
    function fade(id) {
    if(k<=164){
    clearTimeout(Timer);
    window.status=i+" "+j+" "+k;
    document.getElementById(id).style.backgroundColor='rgb('+ i +','+ j +','+ k +')';
    i=i+3;
    j=j+5;
    k=k+5;
    Timer=setTimeout(function() {fade(id)},0);
    }else{
    clearTimeout(Timer);
    }
    }
    
    function fadeout(id) {
    if(k>=0){
    clearTimeout(Timer);
    window.status=i+" "+j+" "+k;
    document.getElementById(id).style.backgroundColor='rgb('+ i +','+ j +','+ k +')';
    i=i-3;
    j=j-5;
    k=k-5;
    Timer=setTimeout(function() {fadeout(id)},0);
    }else{
    clearTimeout(Timer);
    }
    }
    
    </script>
    
    <style>
    a{font-variant:small-caps;
    color:#FFFFFF;
    text-decoration:none;
    padding-left:25px;
    font-weight:bold;
    width:250px;
    border:1px solid white;
    background:url('http://www.singleparadox.com/images/DD_li_over.gif') no-repeat left;
    }
    
    </style>
    
    <body bgcolor="black">
    
    <a id="yus" onmouseover="fade(this.id);" onmouseout="fadeout(this.id);" href="http://www.singleparadox.com">Home</a><br>
    <a id="yus2" onmouseover="fade(this.id);" onmouseout="fadeout(this.id);" href="http://www.singleparadox.com">Links</a><br>
    <a id="yus3" onmouseover="fade(this.id);" onmouseout="fadeout(this.id);" href="http://www.singleparadox.com">Music</a><br>
    <a id="yus4" onmouseover="fade(this.id);" onmouseout="fadeout(this.id);" href="http://www.singleparadox.com">Contact</a><br>
    My goal is to dynamically create the variables I, J, K, and Timer each time it spots a link, so there will be like:

    var I[0];
    var I[1];
    var I[2];

    var J[0];
    var J[1];
    var J[2];

    var K[0];
    var K[1];
    var K[2];

    var Timer[0];
    var Timer[1];
    var Timer[2];

    and so on and so forth, each time a link is spotted on a page. This method is so all the links don't depend on the single I,J,K, and Timer. Does anyone catch my drift? Is this even possible?
    Single Paradox
    My Site (Under construction)

    Not too bad for a 15 year old

  • #2
    New Coder
    Join Date
    Oct 2005
    Posts
    87
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The reason that "joe" was set as undefined is because the "var joe = new Array()" was located inside of a loop. Therefore everytime the loop started, you were reinitializing and clearing the array. Try the code below. Just remove the test portion of the script after you are sure it's working.

    Code:
    <html>
    <head>
    
    <script>
    
    function createVar() {
    var joe=new Array();
    for(i=0;i<document.links.length;i++){
    joe[i]=document.links[i];
    }
    
    //the following is only used to test the script
    var content = "";
    for (j in joe) {
    content += joe[j] + "<br>";
    }
    document.getElementById('test').innerHTML = content;
    
    }
    
    </script>
    
    </head>
    
    <body onload="createVar()">
    
    <a href="test1.htm">test 1</a>
    <a href="test2.htm">test 2</a>
    <a href="test3.htm">test 3</a>
    <a href="test4.htm">test 4</a>
    <a href="test5.htm">test 5</a>
    <a href="test6.htm">test 6</a>
    <a href="test7.htm">test 7</a>
    <a href="test8.htm">test 8</a>
    <a href="test9.htm">test 9</a>
    <a href="test10.htm">test 10</a>
    <a href="test11.htm">test 11</a>
    <a href="test12.htm">test 12</a>
    <a href="test13.htm">test 13</a>
    
    
    <!-- the following is only used to test the script -->
    <br><br><br>
    <span id="test">
    </span>
    
    </body>
    </html>

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    525
    Thanks
    0
    Thanked 0 Times in 0 Posts

    :thumbsup:

    Thank you very much! That worked like a charm, so now there are 13 variables of joe? It is perfect thank you so much!

    -Tom
    Single Paradox
    My Site (Under construction)

    Not too bad for a 15 year old


  •  

    Posting Permissions

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