Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: anyway to make my script count itself?

  1. #1
    Join Date
    Jun 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default anyway to make my script count itself?

    i have a js script that i am using...however i have to go in and edit the number everytime counting up from 0 to say 300...is there anyway to make the script count itself? the number in question is the number after the randomlinks[#]...

    Code:
    var randomlinks=new Array()
     
    randomlinks[1]='<a href="[site]" target="_blank"><img alt="..." src="[img]" style="border: 1px solid #BCBCBC"  width="200" height="150"></a>'
    randomlinks[2]='<a href="[site]" target="_blank"><img alt="..." src="[img]" style="border: 1px solid #BCBCBC"  width="200" height="150"></a>'
    randomlinks[3]='<a href="[site]" target="_blank"><img alt="..." src="[img]" style="border: 1px solid #BCBCBC"  width="200" height="150"></a>'
    randomlinks[4]='<a href="[site]" target="_blank"><img alt="..." src="[img]" style="border: 1px solid #BCBCBC"  width="200" height="150"></a>'
    ...
    
    function randomlink(){
    var add = randomlinks[Math.floor(Math.random()*randomlinks.length)]
    return(add)
    }

  2. #2
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    I can't quite fathom what you're asking. Are you saying periodically you need to add elements to the array but don't want to add an index [n] to each one? If so that's easy, but I suspect this code can be simplified a lot further, especially if the only values that vary are href and src.

  3. #3
    Join Date
    Jun 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    if you can simplify the code by all means...i get code from a dump that allows me to put this code in...

    randomlinks[0]='<a href="[site]" target="_blank"><img alt="..." src="[img]" style="border: 1px solid #BCBCBC" width="200" height="150"></a>'
    the dump can automatically put in the [site], [img], and alt description. I then copy and paste the code into my js script, however i need the radomlinks[0] to count from 0...300...is there anyway to do this?

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,070
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    What dump? Can't it generate the numbers? If so, let it. If not, see if you can make it work like so:

    Code:
    var randomlinks = [
     
    '<a href="[site]" target="_blank"><img alt="..." src="[img]" style="border: 1px solid #BCBCBC"  width="200" height="150"></a>',
    '<a href="[site]" target="_blank"><img alt="..." src="[img]" style="border: 1px solid #BCBCBC"  width="200" height="150"></a>',
    '<a href="[site]" target="_blank"><img alt="..." src="[img]" style="border: 1px solid #BCBCBC"  width="200" height="150"></a>',
    '<a href="[site]" target="_blank"><img alt="..." src="[img]" style="border: 1px solid #BCBCBC"  width="200" height="150"></a>'
    
    ];
    That's just another way of writing an array, no numbers required though. Once it's written, it's length property will be the same as it would have been if each line were numbered, and running:

    Code:
    function randomlink(){
    var add = randomlinks[Math.floor(Math.random()*randomlinks.length)]
    return(add)
    }
    will produce the same sort of result it did before.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    worked! great thank you...you just saved me a lot of time...

    another question...

    using this same code is there anyway to make sure that my links are not repeated?

    Code:
    var randomlinks = [
     
    '<a href="[site]" target="_blank"><img alt="..." src="[img]" style="border: 1px solid #BCBCBC"  width="200" height="150"></a>',
    '<a href="[site]" target="_blank"><img alt="..." src="[img]" style="border: 1px solid #BCBCBC"  width="200" height="150"></a>',
    '<a href="[site]" target="_blank"><img alt="..." src="[img]" style="border: 1px solid #BCBCBC"  width="200" height="150"></a>',
    '<a href="[site]" target="_blank"><img alt="..." src="[img]" style="border: 1px solid #BCBCBC"  width="200" height="150"></a>'
    
    ];
    
    
    function randomlink(){
    var add = randomlinks[Math.floor(Math.random()*randomlinks.length)]
    return(add)
    }

    this is the code embedded in the html file...

    Code:
    <tr>
    <td><script>document.write( randomlink() );</script></td>
    <td><script>document.write( randomlink() );</script></td>
    <td><script>document.write( randomlink() );</script></td>
    <td><script>document.write( randomlink() );</script></td>
    </tr>

  6. #6
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Quote Originally Posted by grendel54 View Post

    another question...

    using this same code is there anyway to make sure that my links are not repeated?
    Replace your randomlink function with this:
    Code:
    function randomlink()
    {
      return randomlinks.splice( Math.floor( Math.random() * randomlinks.length ), 1 )[ 0 ];   
    }
    To make it even easier to maintain, you could call it like this (Once only)
    Code:
    <script>
    
    while( randomlinks.length )
      randomlink();
    
    </script>

  7. #7
    Join Date
    Jun 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by clueful View Post
    Replace your randomlink function with this:
    Code:
    function randomlink()
    {
      return randomlinks.splice( Math.floor( Math.random() * randomlinks.length ), 1 )[ 0 ];   
    }
    I used this code ^^^^ and it worked great...thank you.

    Quote Originally Posted by clueful View Post
    To make it even easier to maintain, you could call it like this (Once only)
    Code:
    <script>
    
    while( randomlinks.length )
      randomlink();
    
    </script>
    unfortunately i cannot use this one as it pulls 36 links from a list of 300.

    i will use this code on a different script though...thanks!

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,070
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    Code:
    var numlinks = -1;
    while(++numlinks < 36){
    	randomlink(); //or whatever you need to do to write them to the page
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Jun 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    the values that vary are the href the src and the alt tags

  10. #10
    Join Date
    Jun 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    Code:
    var numlinks = -1;
    while(++numlinks < 36){
    	randomlink(); //or whatever you need to do to write them to the page
    }
    This code worked great...thank you guys so much

    so this is what it looked like in the end...

    JS FILE...
    Code:
    var randomlinks = [
    
    '<a href="http://[site]">[description]</a><br>',
    '<a href="http://[site]">[description]</a><br>',
    '<a href="http://[site]">[description]</a><br>',
    '<a href="http://[site]">[description]</a><br>'
    
    ];
    
    function randomlink()
    {
      return randomlinks.splice( Math.floor( Math.random() * randomlinks.length ), 1 )[ 0 ];   
    }
    HTML CODE...
    Code:
    <script>
    
    var numlinks = -1;
    while(++numlinks < 36){
    	document.write( randomlink() );
    }
    
    </script>
    and all i have to do is keep adding links to my js script!


    now i need to create a code that changes the links on a daily basis instead of when the page reloads...
    anyone?

Similar Threads

  1. How to make Javascript count-up timer ?
    By k12onos in forum JavaScript
    Replies: 6
    Last Post: 03-27-2012, 11:57 PM
  2. Looking for a count up script
    By darmie_a_Phoenix in forum Dynamic Drive scripts help
    Replies: 24
    Last Post: 03-22-2010, 06:08 PM
  3. Count down script
    By Scrounger in forum Dynamic Drive scripts help
    Replies: 9
    Last Post: 08-08-2007, 09:29 PM
  4. Count Up Script
    By Dan6971 in forum JavaScript
    Replies: 1
    Last Post: 09-07-2005, 03:58 PM
  5. Count Down Script
    By kjenkins in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 12-10-2004, 09:45 PM

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
  •