Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I dynamically change the content in an iframe using jquery?

Tags:

jquery

iframe

I was wondering if it is possible to have a site with an iframe and some jquery code that changes the iframe content every 30 seconds. The content is in different webpages.

Something like this:

<html>   <head>     <script type="text/javascript" src="jquery.js"></script>     <script>       $(document).ready(function(){         var array = new array();         array[0] = 'http://webPage1.com';         array[1] = 'http://webPage2.com';         // And so on.         // Do something here to change the iframe every 30 second       });     </script>   </head>   <body>     <iframe id="frame"></iframe>   </body> </html> 
like image 294
Audun Avatar asked Oct 12 '09 12:10

Audun


People also ask

How do I change iFrame content dynamically?

You can use the following to change the src attribute of the iFrame: $("#content"). attr('src', 'http://mysite.com/newpage.html');


2 Answers

<html>   <head>     <script type="text/javascript" src="jquery.js"></script>     <script>       $(document).ready(function(){         var locations = ["http://webPage1.com", "http://webPage2.com"];         var len = locations.length;         var iframe = $('#frame');         var i = 0;         setInterval(function () {             iframe.attr('src', locations[++i % len]);         }, 30000);       });     </script>   </head>   <body>     <iframe id="frame"></iframe>   </body> </html> 
like image 164
Anatoliy Avatar answered Sep 25 '22 13:09

Anatoliy


If you just want to change where the iframe points to and not the actual content inside the iframe, you would just need to change the src attribute.

 $("#myiframe").attr("src", "newwebpage.html"); 
like image 37
Anthony Avatar answered Sep 26 '22 13:09

Anthony