I\'m trying to create a fairly simple piece of JavaScript that displays a random image from an array each time the page loads. I need to figure out a way to get this runnin
Just don't put you javascript inside a function.
if you take it out of the function it will run when the page loads.
When it's in the function, it won't run unless called.
Your answer can be found at glados.cc/myfaucet.
there is a script for download there which has the code you are looking for
here is a brief synopsis:
create file config.php and put this inside:
<?php
// Advertisement Codes
// All ads rotate. There are 3 types: square, text, and banner. Add HTML code to the array
$squareAds = array('<iframe scrolling="no" style="border: 0; width: 250px; height: 250px;" src="http://myadserver.com"></iframe>');
$textAds = array('<p><strong><a href="http://yoururl.com">YourURL</a></strong></p>', '<p><strong>Get your own site</strong> <a href="http://yoursite.com">here</a></p>');
$bannerAds = array('<iframe scrolling="no" style="border: 0; width: 468px; height: 60px;" src="http://youradserver.com"></iframe>
');
?>
Then, on the page you are serving, say, index.php to display your random links or images or text simply put:
<?php
require_once('config.php');
function getAd($arr){
return $arr[rand(0, count($arr)-1)];
}
echo "any plain text or html" . getAd($textAds) . "any plain text or html" . getAd($bannerAds) . "any plain text or html" . getAd($squareAds) . "any plain text or html";
?>
Of course, you can create as many $xxxAds arrays and name them whatever you want, and you can fill in the arrays with whatever html or jscript to be displayed, as well as an unlimited number per array, in csv format you can also create as many new "getAd" functions with different names so that you can have multiple unique iframe ads being displayed on one page
With a few changes this code will load images randomly and his respective link to load.
<!DOCTYPE html>
<html>
<head>
<title>Jorgesys Html test</title>
<script type="text/javascript">
var imageUrls = [
"http://prueba.agenciareforma.com/appiphone/android/img/newspapers/stackoverflow.png"
, "http://ww.mydomain.com/img/newspapers/reforma.png"
, "http://ww.mydomain.com/img/newspapers/nytimes.png"
, "http://ww.mydomain.com/img/newspapers/oglobo.png"
, "http://ww.mydomain.com/img/newspapers/lefigaro.png"
, "http://ww.mydomain.com/img/newspapers/spiegel.png"
];
var imageLinks = [
"http://www.stackoverflow.com"
, "http://www.reforma.com"
, "http://www.nytimes.com/"
, "https://oglobo.globo.com/"
, "http://www.lefigaro.fr/international/"
, "http://www.spiegel.de/international/"
];
function getImageHtmlCode() {
var dataIndex = Math.floor(Math.random() * imageUrls.length);
var img = '<a href=\"' + imageLinks[dataIndex] + '"><img src="';
img += imageUrls[dataIndex];
img += '\" alt=\"Jorgesys Stackoverflow.com\"/></a>';
return img;
}
</script>
</head>
<body bgcolor="white">
<script type="text/javascript">
document.write(getImageHtmlCode());
</script>
</body>
</html>