PDA

View Full Version : Help with Random iframe content



surreal_drop
04-22-2007, 05:08 AM
1) Script Title: Random iframe content

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex17/randomiframe.htm

3) Describe problem:
I would like to use this script many times on the same page. I've been playing around with it a bit but can't seem to get it to work.

I've changed the iframe id="dynstuff" to something unique for each place I use it... but it still isn't working.

I hope there is an easy/quick way to accomplish this.

Thanks,
~D

surreal_drop
04-22-2007, 05:54 AM
Yeeep!

I just read this thread: http://www.dynamicdrive.com/forums/showthread.php?t=320

If anyone can offer me any help I would greatly appreciate it... If it is going to be super time consuming then dont worry.

Thanks,
~D :)

jscheuer1
04-22-2007, 06:48 AM
This is actually one of the easy ones:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">

//Random iframes content - &#169; Dynamic Drive (www.dynamicdrive.com)
//For full source code, and Terms Of use, visit http://dynamicdrive.com
//This credit MUST stay intact for legal use
//Updated in http://dynamicdrive.com/forums for multiple use by:
//John Davenport Scheuer (username:jscheuer1)


//Specify random URLs to display inside iframe
var randomcontent=new Array()
randomcontent[0]="random1.htm"
randomcontent[1]="random2.htm"
randomcontent[2]="random3.htm"
randomcontent[3]="random4.htm"
//Specify this IFRAME's display attributes
randomcontent.iframeprops='width=130 height=130 marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="1" scrolling="no"'

//Specify random URLs to display inside another iframe (add as many of these as you want)
var randomcontent2=new Array()
randomcontent2[0]="random5.htm"
randomcontent2[1]="random6.htm"
randomcontent2[2]="random7.htm"
randomcontent2[3]="random8.htm"
//Specify this IFRAME's display attributes
randomcontent2.iframeprops='width=300 height=250 marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="auto"'

//No need to edit after here
function random_iframe(){
if (!document.all&&!document.getElementById)
return;
this.content=arguments[0];
random_iframe.num=random_iframe.num? random_iframe.num+1 : 1;
this.frame_id='dynstuff'+random_iframe.num;
document.write('<iframe id="'+this.frame_id+'" src="" '+this.content.iframeprops+'></iframe>');
this.frame=document.getElementById? document.getElementById(this.frame_id) : document.all[this.frame_id];
var cacheobj=this;
this.onloadfunc=function(){
cacheobj.frame.src=cacheobj.content[Math.floor(Math.random()*cacheobj.content.length)]
};
if ( typeof window.addEventListener != "undefined" )
window.addEventListener( "load", this.onloadfunc, false );
else if ( typeof window.attachEvent != "undefined" )
window.attachEvent( "onload", this.onloadfunc );
else {
if ( window.onload != null ) {
var oldOnload = window.onload;
window.onload = function ( e ) {
oldOnload( e );
cacheobj.onloadfunc();
};
}
else
window.onload = this.onloadfunc;
}
}

</script>

</head>
<body>
<script type="text/javascript">
new random_iframe(randomcontent)
</script><br>
<script type="text/javascript">
new random_iframe(randomcontent2)
</script><br>
</body>
</html>

Notes: The main script now goes in the head. There is a different order for the array and frame properties and slightly different format for defining the iframe properties. Each iframe is called with a small script in the body. If you like, you can define just one set of pages (array) and its iframe properties and use it over and over again in the body of the page.

surreal_drop
04-22-2007, 04:30 PM
Perfect!

Works like a charm.
Thank you so much. I really appreciate it.

~D

5starmember
07-25-2008, 07:23 PM
THANKS! I needed this too! I'm so happy!