0

What I'm trying to achieve is have a random URL selected, then that URL's page displayed in an embed that you can then click and go to that page. I've been having trouble however and would appreciate any help. This is what I got so far:

<script type="text/javascript">

var urls = new Array();
urls[0] = "https://www.google.com";
urls[1] = "https://www.yahoo.com";
urls[2] = "https://www.bing.com";

var random = Math.floor(Math.random()*urls.length);

window.location = urls[random];

<embed src="urls" style="width:500px; height: 300px;">
</script>
adam
  • 67
  • 7
  • the `` tag can't be put inside the ` – Esger May 08 '19 at 20:09

1 Answers1

1

There are many errors in your code, for example the fact that you set the location of the current window with window.location = ... instead of setting the URL of the <iframe>, or that you have HTML code inside your JS, etc.

In any case, you should chose the URL before and then add an <iframe> to the DOM and set its url. Here's an example:

const urls = [
  'https://example.com',
  'https://example.org',
];

const iframe = document.createElement('iframe');
iframe.src = urls[Math.floor(Math.random()*urls.length)];
document.body.appendChild(iframe);
Nino Filiu
  • 16,660
  • 11
  • 54
  • 84