0

I’ve got a random image script that works fine. I’m trying to open a link in a specific window size which doesn’t want to work and applied to the random image link.

It works fine if it’s not in the random image script.

Is there any way to make it work within the random image script?

Here is my code:

<SCRIPT LANGUAGE="Javascript">
function banner() { } ; b = new banner() ; n = 0

b[n++]= "<a href='https://youtu.be/IeIqaNR5Zs4' target='_blank'    
onclick='window.open(href,                '', 'height=300, width=450,  
left=300, top=300'); return false;'> <IMG name=randimg 
SRC='images/random/balboapark.png' alt='Balboa Park Picture'>"

b[n++]= "<a href='https://www.youtube.com/watch?v=w6UzpzMwwT8'> <IMG  
name=randimg SRC='images/random/zoo.png' alt='San Diego Zoo Picture'>"

b[n++]= "<a href='https://www.youtube.com/watch?v=w6UzpzMwwT8'> <IMG 
name=randimg SRC='images/random/zoo.png' alt='San Diego Beach Picture'>"

i=Math.floor(Math.random() * n) ; 
document.write( b[i] )
</SCRIPT>



This opens in the specific window size.

<a target="_blank" href="https://youtu.be/IeIqaNR5Zs4" 
onclick="window.open    (href,                '', 'height=300, width=450, 
left=300, top=300'); return false;"><IMG SRC='images/random/balboapark.png' 
alt='Balboa Park Picture'></a>
Ike Evens
  • 13
  • 5

1 Answers1

2

If I understand your question correctly, then this code should achieve what you require. Rather than use document.write(), this approach interacts with the DOM API directly, and also uses an event listener for the click event to trigger the window.open().

Please note that this code won't work in stack overflows snippet "sandbox" so you will need to try this solution in your own project directly:

// Function configures DOM elements for a single banner based 
// on provided input parameters
function createBanner(url, src, alt) {

  // Create the image and assign alt/src attributes
  let img = document.createElement('img');
  img.setAttribute('src', src);
  img.setAttribute('alt', alt);

  // Create the anchor, assign attributes and click listener
  let a = document.createElement('a');
  a.setAttribute('href', url);
  a.addEventListener('click', function() {

    window.open(url, '', 'height=300, width=450, left=300, top=300');
    return false
  });

  // Add image to anchor
  a.appendChild(img);

  // Return anchor for external use
  return a
}

// Create banners from your input data
[
  createBanner('https://youtu.be/IeIqaNR5Zs4', 'images/random/balboapark.png', 'Balboa Park Picture'),
  createBanner('https://youtu.be/w6UzpzMwwT8', 'images/random/zoo.png', 'San Diego Zoo Picture'),
  createBanner('https://youtu.be/w6UzpzMwwT8', 'images/random/beach.png', 'San Diego Beach Picture'),
]
.forEach(function(e, i, array) {

  // Iterate through each banner, and for each banner, 
  // select a random banner from the collection to be
  // added to the DOM
  const banner = array[Math.floor(Math.random() * array.length)];
  
  document.body.appendChild(banner);
})
Dacre Denny
  • 29,664
  • 5
  • 45
  • 65