Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Help for code

  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help for code

    Hello,
    I have this code:
    .......................

    Code:
    <script language="javascript">
    var delay=1000 //set delay in miliseconds
    var curindex=0
    
    var randomimages=new Array()
    
    	randomimages[0]="1.jpg"
    	randomimages[1]="5.jpg"
    	randomimages[2]="2.jpg"
    	randomimages[3]="4.jpg"
    	randomimages[4]="3.jpg"
    	randomimages[5]="6.jpg"
    
    var preload=new Array()
    
    for (n=0;n<randomimages.length;n++)
    {
    	preload[n]=new Image()
    	preload[n].src=randomimages[n]
    }
    
    document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*(randomimages.length))]+'">')
    
    function rotateimage()
    {
    
    if (curindex==(tempindex=Math.floor(Math.random()*(randomimages.length)))){
    curindex=curindex==0? 1 : curindex-1
    }
    else
    curindex=tempindex
    
    	document.images.defaultimage.src=randomimages[curindex]
    }
    
    setInterval("rotateimage()",delay)
    
    </script>
    ....................................
    I would like to add url link to each image.
    How can i do that?
    Many thank to help me.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Then you should change the document.write line to include an <a> anchor tag warpped around the <img> tag

    Something in the lines of
    Code:
    document.write('<a href="whatever.html"><img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*(randomimages.length))]+'"></a>');

  • #3
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks devnull69 but i want a different url for each iumage...so how can i do that.
    Many thanks in advance.

  • #4
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    You did a randomimages array; make a randomURL array, and use it to populate the href of the anchor.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #5
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks WolfShade but how do you do that...i have very limited knowledge of javascript so please help me.

    Could you post the code.
    many thanks.

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Code:
    var randomURL=new Array()
    
    	randomURL[0]="whatever1.html"
    	randomURL[1]="http://www.whatelse.com/whatever2.html"
    	randomURL[2]="whatever3.php"
    	randomURL[3]="something.html"
    	randomURL[4]="where/what.html"
    	randomURL[5]="whatever4.html"
    
    ...
    
    
    var idx = Math.floor(Math.random()*(randomimages.length));
    document.write('<a href="' + randomURL[idx] + '"><img name="defaultimage" src="'+randomimages[idx]+'"></a>');

  • #7
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    many thank


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •