Javascript open link from an array in new window

Status
Not open for further replies.

ISP_236

Wise, Aged Ars Veteran
162
Probably an easy question.

I am working from the code posted on the following http://www.htmlgoodies.com/beyond/j...l-Adding-Rotating-Images-to-Your-Web-Site.htm for an image rotator using javascript.

What I want to do is to be able is open the link that is assoicated with an image stored in the array in a new window when someone clicks the image that loads on the page.

The original code posted:

Code:
<script language="JavaScript1.2">

var howOften = 5; //number often in seconds to rotate
var current = 0; //start the counter at 0
var ns6 = document.getElementById&&!document.all; //detect netscape 6

// place your images, text, etc in the array elements here
var items = new Array();
    items[0]="<a href='link.htm' ><img alt='image0 (9K)' src=' /html/Images/image0.jpg' height='300' width='300' border='0' /></a>"; //a linked image
    items[1]="<a href='link.htm'><img alt='image1 (9K)' src='/html/Images/image1.jpg' height='300' width='300' border='0' /></a>"; //a linked image
    items[2]="<a href='link.htm'><img alt='image2 (9K)' src='/html/Images/image2.jpg' height='300' width='300' border='0' /></a>"; //a linked image
   items[3]="<a href='link.htm'><img alt='image3 (9K)' src='/html/Images/image3.jpg' height='300' width='300' border='0' /></a>"; //a linked image
    items[4]="<a href='link.htm'><img alt='image4 (9K)' src='/html/Images/image4.jpg' height='300' width='300' border='0' /></a>"; //a linked image
    items[5]="<a href='link.htm'><img alt='image5 (18K)' src='/html/Images/image5.jpg' height='300' width='300' border='0' /></a>"; //a linked image
function rotater() {
    document.getElementById("placeholder").innerHTML = items[current];
    current = (current==items.length-1) ? 0 : current + 1;
    setTimeout("rotater()",howOften*1000);
}

function rotater() {
    if(document.layers) {
        document.placeholderlayer.document.write(items[current]);
        document.placeholderlayer.document.close();
    }
    if(ns6)document.getElementById("placeholderdiv").innerHTML=items[current]
        if(document.all)
            placeholderdiv.innerHTML=items[current];

    current = (current==items.length-1) ? 0 : current + 1; //increment or reset
    setTimeout("rotater()",howOften*1000);
}
window.onload=rotater;
//-->
</script>

Thanks.
 

inpher

Well-known member
3,697
In the spirit of Zanshin's question why you didn't use jQuery I have made something for you:

Code:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    // Number in milliseconds to pause each image
    var pause = 5000;

    // Start the counter
    var current = 0;
    
    // The speed at which to animate
    var speed = 500;
    
    // Place your images, text, etc in the array elements here
    var items = $("#my_container a");
    
    // Bind click on link to open the link in a new window
    $(document).on('click', "#my_container a", function() {
        $(this).attr('target', '_blank');
    });
    
    // On load hide every image except the first
    $(items).not(":first").hide();
    
    setTimeout(rotate, pause);
    function rotate() {
        // Hides the currently visible item
        items.eq(current).animate({opacity:'toggle'}, speed);
        
        // Count to the next and start again from the beginning if at the end
        if (current++ >= items.length) {
            current = 0;
        }
        
        // Shows the next item in the list
        items.eq(current).animate({opacity:'toggle'}, speed);
        
        // Again, we will do it!
        setTimeout(rotate, pause);
    }

});
</script>
<style media="all">
#my_container img {
    border: none;
    height: 300px;
    width: 300px;
}
</style>

<div id="my_container">
    <a href='link.htm'><img alt='image0 (9K)' src='Images/image0.jpg'></a>
    <a href='link.htm'><img alt='image1 (9K)' src='Images/image1.jpg'></a>
    <a href='link.htm'><img alt='image2 (9K)' src='Images/image2.jpg'></a>
    <a href='link.htm'><img alt='image3 (9K)' src='Images/image3.jpg'></a>
    <a href='link.htm'><img alt='image4 (9K)' src='Images/image4.jpg'></a>
    <a href='link.htm'><img alt='image5 (18K)' src='Images/image5.jpg'></a>
</div>

This is bare bones, but as you see I moved the elements to the actual document, moved style attributes to the style element. You should also style your images to an absolute position, this might require the container element to be positioned as relative, to have them fade on top of each other.
 

ISP_236

Wise, Aged Ars Veteran
162
Inpher - Thanks for the your reply and the code. As I stated in my reply to Zanshin I am not a programmer/web developer. My skills are limited in that area. I was asked to fix something on our website that was broken and in my search to find a new image rotator component the link/code from my original post was what I found and could get to work. It was an emergency fix. The minor issue was that when someone clicked on the picture the link did not open in a new window. I understand that the code is outdated and the process could be written better. Hopefully I can look at jQuery more at a later date.
 
Status
Not open for further replies.