Results 1 to 10 of 10

Thread: Mootools random image?

  1. #1
    Join Date
    Sep 2008
    Posts
    77
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Mootools random image?

    Is there such thing as a random image script that can work along side mootools?

    Thanks,
    John

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

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

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

  3. #3
    Join Date
    Sep 2008
    Posts
    77
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    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?

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Provide the code, please.
    Jeremy | jfein.net

  5. #5
    Join Date
    Sep 2008
    Posts
    77
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    PHP Code:
    <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.

  6. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    It appears that that script will only show an image if the random number equals 0. Try:
    Code:
    <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>
    Jeremy | jfein.net

  7. #7
    Join Date
    Sep 2008
    Posts
    77
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Nile View Post
    It appears that that script will only show an image if the random number equals 0. Try:
    Code:
    <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.

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    <!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.
    - John
    ________________________

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

  9. #9
    Join Date
    Sep 2008
    Posts
    77
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    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:

    Code:
    <!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?

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

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

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

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
  •