Rotary Iframe in JavaScript

1

I would like to fix the following script to stay the way I need it.

<html>
<head>
<script>
function randomIframe(obj){
var ends = new Array();
ends[0] = "http://www.terra.com.br";
ends[1] = "http://www.uol.com.br";
ends[2] = "http://www.bol.com.br";
ends[3] = "http://www.cade.com.br";
ends[4] = "http://www.baixaki.com.br";

var i = Math.round(Math.random()*ends.length-1);

obj.location.replace(ends[i]);

}

</script>
</head>
<body onload="randomIframe(publicidade)">

<iframe name="publicidade" width="500" height="100"></iframe>

</body>
</html>

I was wondering if there is some way for <iframe> to be inside the script , not below <body> , something that uses document.write inside script and window.onload = random_iframe for example. To get only something inside <script> to </script> .

    
asked by anonymous 26.06.2015 / 15:35

1 answer

0

I used jquery to load in iframe see:

function randomIframe(obj){
var ends = new Array();
  
ends[0] = "http://www.terra.com.br";
ends[1] = "http://www.uol.com.br";
ends[2] = "http://www.bol.com.br";
ends[3] = "http://www.cade.com.br";
ends[4] = "http://www.baixaki.com.br";
var i = Math.round(Math.random()*ends.length-1);
obj.location.replace(ends[i]);
  
}
$(document).ready(function () {
  
    $("#publicidade").load(randomIframe(this));    
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script><iframename="publicidade" width="500" height="100" id='publicidade'></iframe>

If you want it to open in the size specified by your iframe, use the src attribute to put the path of an html that will open the entire iframe page, eg

// Pagina que contem o iframe

<!DOCTYPE html>
<html>
   <head>
      <title>Exemplo</title>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">

   </head>
   <body>
      <iframe name="publicidade" src='teste2.html' id='publicidade' height="300" width="500"></iframe>
   </body>
</html>

// Pagina que carregará todo site

<!DOCTYPE html>
<html>
   <head>
      <title>Exemplo</title>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">

   </head>
   <body>

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script><script>functionrandomIframe(obj){varends=newArray();ends[0]="http://www.terra.com.br";
            ends[1] = "http://www.uol.com.br";
            ends[2] = "http://www.bol.com.br";
            ends[3] = "http://www.cade.com.br";
            ends[4] = "http://www.baixaki.com.br";
            var i = Math.round(Math.random() * ends.length - 1);
            obj.location.replace(ends[i]);
         }
         $(document).ready(function () {
            $("body").load(randomIframe(this));
         });
      </script>
   </body>
</html>
    
26.06.2015 / 16:00