PDA

View Full Version : anyway to make my script count itself?



grendel54
06-23-2013, 07:39 PM
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[#]...


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)
}

clueful
06-23-2013, 07:56 PM
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.

grendel54
06-23-2013, 08:00 PM
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?

jscheuer1
06-24-2013, 03:11 AM
What dump? Can't it generate the numbers? If so, let it. If not, see if you can make it work like so:


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:


function randomlink(){
var add = randomlinks[Math.floor(Math.random()*randomlinks.length)]
return(add)
}

will produce the same sort of result it did before.

grendel54
06-24-2013, 05:45 PM
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?


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...


<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>

clueful
06-24-2013, 06:21 PM
another question...

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



Replace your randomlink function with this:

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)

<script>

while( randomlinks.length )
randomlink();

</script>

grendel54
06-25-2013, 07:16 PM
Replace your randomlink function with this:

function randomlink()
{
return randomlinks.splice( Math.floor( Math.random() * randomlinks.length ), 1 )[ 0 ];
}

I used this code ^^^^ and it worked great...thank you.


To make it even easier to maintain, you could call it like this (Once only)

<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!

jscheuer1
06-25-2013, 08:22 PM
var numlinks = -1;
while(++numlinks < 36){
randomlink(); //or whatever you need to do to write them to the page
}

grendel54
07-03-2013, 04:55 AM
the values that vary are the href the src and the alt tags

grendel54
07-03-2013, 05:18 AM
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...


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...

<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?

jscheuer1
07-03-2013, 05:36 AM
To do that with javascript alone you would have to make up an array of 36 or more links for each day of the week (7 arrays), or each day of the month (31 arrays).

But if you were to go partially or fully server side, you could have a random set of links from a single huge array and have that set of links change on a daily basis until all were all used up, then start over from the complete list.

grendel54
07-03-2013, 09:14 PM
but would i be able to keep them as links?


<a href="[site]>[desc]</a>

or would it be just a list of [sites] only?


also...i thought there was a way to use the links on a daily basis until all were used up using a js script...

possibly changing the (randomlink.length ) code to something different?


you could have a random set of links from a single huge array and have that set of links change on a daily basis until all were all used up, then start over from the complete list.

this is exactly what im looking to do

jscheuer1
07-03-2013, 09:59 PM
For that last, you would have to go server side. The only way to do it in javascript alone would involve persistent cookies and/or local storage, but folks could delete those/that or not accept them in the first place. Even if they didn't choose to, default data limits on some browsers might make it break down.

When I have a bit more time I will draw something up for a server side approach. But you will need a host that runs server side code. I can do PHP, possibly asp. I prefer PHP. Does your host have PHP available to you?

Oh, and - Yes, server side code can supply full links on the page.

grendel54
07-03-2013, 11:44 PM
For that last, you would have to go server side. The only way to do it in javascript alone would involve persistent cookies and/or local storage, but folks could delete those/that or not accept them in the first place. Even if they didn't choose to, default data limits on some browsers might make it break down.

When I have a bit more time I will draw something up for a server side approach. But you will need a host that runs server side code. I can do PHP, possibly asp. I prefer PHP. Does your host have PHP available to you?

Oh, and - Yes, server side code can supply full links on the page.

PHP 5.2 or 5.3

Thank you very much...

I will check into PHP as well. I learned some PHP in school but not using it, it has faded...

jscheuer1
07-04-2013, 05:18 PM
OK, here's the main code:


<?php
$numlinks = 5; //set to number of links to display today - this number should evenly divide into the number of links listed in the $masterfile
$masterfile = 'masterlinks.txt'; //create and maintain this file yourself, list one link per line, no blank lines, except one at the end
$currentfile = 'currentlinks.txt'; //do not create or maintain this file, the script will do that
$dayfile = 'day.txt'; //do not create or maintain this file, the script will do that
clearstatcache($dayfile);
$day = is_file($dayfile)? date("j", filemtime($dayfile)) : 0;
$today = date("j");
clearstatcache($currentfile);
//ini_set("auto_detect_line_endings", true); //uncomment this line if $masterfile was made on a Mac, or server is a Mac
$currentlinks = is_file($currentfile)? file($currentfile) : null;
if(!$currentlinks || count($currentlinks) < $numlinks){
copy($masterfile, $currentfile);
$currentlinks = file($currentfile);
}
if($day !== $today){
shuffle($currentlinks);
$todaylinks = array_splice($currentlinks, 0, $numlinks);
file_put_contents($dayfile, $todaylinks);
file_put_contents($currentfile, $currentlinks);
}
echo implode("<br>\n", file($dayfile, FILE_IGNORE_NEW_LINES));
?>



As commented, you only have to make up and maintain the $masterfile (masterlinks.txt in this case). You can change the filenames to suit and include paths if desired. If you do, use the server path to the file, not the URL path. But there's no need to change them. As is the masterlinks.txt file would go in the same folder as this file or, if this file is included on another page, in the same folder as that page.

The masterlinks.txt file should have 1 link per line in ordinary HTML code, ex:


<a href="http://www.google.com/search?hl=en&amp;rls=en&amp;q=bob">Bob</a>
<a href="http://www.google.com/search?hl=en&amp;rls=en&amp;q=carol">Carol</a>
<a href="http://www.google.com/search?hl=en&amp;rls=en&amp;q=ted">Ted</a>
<a href="http://www.google.com/search?hl=en&amp;rls=en&amp;q=alice">Alice</a>
<a href="http://www.google.com/search?hl=en&amp;rls=en&amp;q=jquery">jQuery</a>
<a href="http://www.google.com/search?hl=en&amp;rls=en&amp;q=scriptaculous">Scriptaculous</a>



There should be no blank lines except that the last line should be blank. In other words, there should be a line break at the end of each line that has a link on it. You can use any valid HTML code for a link, including one that contains an image instead of text as the link trigger. Don't quote these links like entries in an array, just write them as normal HTML code, one per line. Image and link paths should be valid for the page that the images and links will appear on. When in doubt, use the absolute path.

The only other thing to configure is the number of daily links ($numlinks). I used 5 as I only had 30 links in my masterlinks.txt file. Unless you want some links to be missed on occasion, that is after going through them all on a day to day basis and resetting because there aren't enough for the last day, use a $numlinks value that evenly divides into the number of links in the masterlinks.txt file. If you don't make it evenly divisible, the links that are missed will be potentially different each cycle, but as luck might have it, one or more might get missed a lot. It's a random algorithm, not a unique one.

Oh, and notice the ini_set() command. It's commented out. The manual said to use that for files created on a Mac and/or for Mac servers. But it also said not to unless there were problems with detecting EOL (line breaks). You will know if that happens if everything else is set up correctly and things still don't work.

Any questions, just let me know.