Random URL for Iframe on page load

i'm trying to load random iframe on my website i got a script

var cat1 = [
    "http://arborjs.org",
    "http://cartodb.com",
    "http://vis4.net/labs/185"
    ];

var myFrame = document.getElementById("frame");

function getRandomUrl(myFrame) {
   var index = Math.floor(Math.random()*cat1.length);
   var url = cat1[index];
   myFrame.src = url;
}

btn.addEventListener("click", function() {
    
   getRandomUrl(myFrame); 
    
});
<button id="btn">Click</button>
<br>
<iframe id="frame" src="" style="width:200px; height: 200px"></iframe>

It works fine when i click the button, but i also want to load random iframe when the page finishes to load.

Can anyone edit this code for me ? thank you

Upvotes: 0

Views: 1500

Answers (1)

Ronen Cypis
Ronen Cypis

Reputation: 21470

widnow.onload is your friend in this case:

window.onload = function(){
    getRandomUrl(myFrame);
};

Upvotes: 2

Related Questions