View Full Version : Mootools random image?
jzhang1013
11-14-2009, 06:20 PM
Is there such thing as a random image script that can work along side mootools?
Thanks,
John
jscheuer1
11-14-2009, 07:19 PM
A 'random image script' isn't very specific. And I'm not sure what you mean by 'along side', though that would literally mean on the same page as mootools, but without using mootools.
The answer in any case though is yes. There are scripts where random images are involved that can work on a page that also has mootools installed. There probably are even mootools based random image scripts.
What exactly did you have in mind? Do you have a random image script that does what you want but just doesn't work on a page with mootools?
jzhang1013
11-16-2009, 03:15 AM
This:
I have a random image script that does what you want but just doesn't work on a page with mootools.
What are some solutions?
Provide the code, please.
jzhang1013
11-17-2009, 12:16 AM
<script language="JavaScript">
<!--
function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="image1.gif"
myimages[2]="image2.gif"
myimages[3]="image3.gif"
myimages[4]="image4.gif"
myimages[5]="image5.gif"
myimages[6]="image6.gif"
var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<img src="'+myimages[ry]+'" border=0>')
}
random_imglink()
//-->
</script>
Or you guys can suggest me one that already works along side mootools.
It appears that that script will only show an image if the random number equals 0. Try:
<script type="text/javascript">
var myimages = new Array();
myimages[0]="image1.gif"
myimages[1]="image2.gif"
myimages[2]="image3.gif"
myimages[3]="image4.gif"
myimages[4]="image5.gif"
myimages[5]="image6.gif"
var randomImg = function(){
var random = Math.floor(Math.random()*myimages.length);
document.write('<img src="'+myimages[random]+'" border=0>');
};
</script>
jzhang1013
11-17-2009, 06:35 AM
It appears that that script will only show an image if the random number equals 0. Try:
<script type="text/javascript">
var myimages = new Array();
myimages[0]="image1.gif"
myimages[1]="image2.gif"
myimages[2]="image3.gif"
myimages[3]="image4.gif"
myimages[4]="image5.gif"
myimages[5]="image6.gif"
var randomImg = function(){
var random = Math.floor(Math.random()*myimages.length);
document.write('<img src="'+myimages[random]+'" border=0>');
};
</script>
That doesn't seem to work with mootools running too.
jscheuer1
11-17-2009, 06:52 AM
Although the code for the random image script is questionable IMO, too exposed, sloppy, and uses document.write unnecessarily, it does work here with mootools in IE 7 and Firefox 3.5:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/mootools/1.2.4/mootools-yui-compressed.js"></script>
<script type="text/javascript">
var myimages = new Array();
myimages[0]="image1.gif"
myimages[1]="image2.gif"
myimages[2]="image3.gif"
myimages[3]="image4.gif"
myimages[4]="image5.gif"
myimages[5]="image6.gif"
var randomImg = function(){
var random = Math.floor(Math.random()*myimages.length);
document.write('<img src="'+myimages[random]+'" border=0>');
};
</script>
</head>
<body>
<script type="text/javascript">
randomImg();
</script>
</body>
</html>
What browser are you using?
If you want more help:
Please post a link to the page on your site that contains the problematic code so we can check it out.
jzhang1013
11-28-2009, 07:11 PM
Although the code for the random image script is questionable IMO, too exposed, sloppy, and uses document.write unnecessarily, it does work here with mootools in IE 7 and Firefox 3.5:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/mootools/1.2.4/mootools-yui-compressed.js"></script>
<script type="text/javascript">
var myimages = new Array();
myimages[0]="image1.gif"
myimages[1]="image2.gif"
myimages[2]="image3.gif"
myimages[3]="image4.gif"
myimages[4]="image5.gif"
myimages[5]="image6.gif"
var randomImg = function(){
var random = Math.floor(Math.random()*myimages.length);
document.write('<img src="'+myimages[random]+'" border=0>');
};
</script>
</head>
<body>
<script type="text/javascript">
randomImg();
</script>
</body>
</html>
What browser are you using?
If you want more help:
Please post a link to the page on your site that contains the problematic code so we can check it out.
Thanks so much this works!! Now if I wanted it to grab the images from a certain directory/folder, how would I be able to do that so I dont have to code out all the images?
jscheuer1
11-28-2009, 07:38 PM
If you have PHP, you could use that to open the folder that has the images in it and fetch all the files in that folder that are images by testing their extensions. But this requires a PHP enabled server, and will not work if the opendir and/or readdir functions are disabled as is sometimes the case. It also won't work locally unless you have a a local PHP sandbox of some kind to test it in. But if your server does have PHP with those functions enabled, the whole thing could be done in PHP. Even without those functions, this all PHP script would work on a PHP enabled server:
http://www.totallyphp.co.uk/scripts/random_image.htm
But you would have to setup the filenames in a certain way and configure in the script how many there are, their extension, etc.
But with those functions, files of any extension(s) that you choose (like .jpg, .gif, .png, .jpeg, etc. - upper and lower case) could be fetched and their names wouldn't matter.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.