Javascript rotatory iframe

Asked

Viewed 51 times

1

I would like to correct the following script to stay the way I need.

<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’s any way <iframe> stay within oneself script, not below the <body>, something that uses document.write within the script and a window.onload = random_iframe for example. To stay only something inside the <script> up to the </script>.

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>
<iframe name="publicidade" width="500" height="100" id='publicidade'></iframe>

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

// 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>
         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 () {
            $("body").load(randomIframe(this));
         });
      </script>
   </body>
</html>

  • Great solution, however the sites are not opening within iframe, the entire page is redirected... Also, is it possible to leave iframe inside the first script with "Document.write"? For example Document.write('<iframe name="publicid....) Thank you

  • edited, saved in an html and forehead, remembering that are 2 pages one that will load everything and another that contains the iframe with the desired resolution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.