...

View Full Version : Random iframe



KiwiGuy
11-21-2012, 10:33 PM
Hi

I got this script online which is meant to display random iframes onload. This script works for me locally, however when uploading and testing online it does not work. It displays absolutely nothing. I tested this with Firefox and IE.

I'm an amateur with Javascript so if someone could spot why this is happening that would be awesome!

Thanks

<script language="JavaScript1.2">

var ie=document.all&&navigator.userAgent.indexOf("Opera")==-1
var dom=document.getElementById&&navigator.userAgent.indexOf("Opera")==-1

var iframeprops='width=130 height=130 marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="1" scrolling="no"'

var randomcontent=new Array()
randomcontent[0]="random1.htm"
randomcontent[1]="random2.htm"
randomcontent[2]="random3.htm"
randomcontent[3]="random4.htm"

if (ie||dom)
document.write('<iframe id="dynstuff" src="" '+iframeprops+'></iframe>')

function random_iframe(){
if (ie||dom){
var iframeobj=document.getElementById? document.getElementById("dynstuff") : document.all.dynstuff
iframeobj.src=randomcontent[Math.floor(Math.random()*randomcontent.length)]
}
}

window.onload=random_iframe
</script>

Old Pedant
11-21-2012, 11:09 PM
Um...that script is ancient history.

It's pretty much as obsolete as you can get.

Let's rewrite it...



<!DOCTYPE html>
<html>
<body>
Here is some stuff not in the iframe.<br/><br/>
Here is some stuff not in the iframe.<br/><br/>
Here is some stuff not in the iframe.<br/><br/>
Here is some stuff not in the iframe.<br/><br/>

<iframe id="dynstuff" width="100%" height="130" marginwidth="0" marginheight="0"
hspace="0" vspace="0" frameborder="1" scrolling="no">
</iframe>

<script type="text/javascript">
(
function( )
{
var randomcontent = [
"http://www.codingforums.com",
"http://www.espn.com",
"http://www.google.com",
"http://www.cnn.com"
];

var theFrame = document.getElementById("dynstuff");

var which = Math.floor( Math.random() * randomcontent.length );

theFrame.src = randomcontent[which];
}
)();
</script>
</body>
</html>


I did just realize that if you didn't upload all those "randomX.html" pages to your online site the code would never have worked. Could that have been the problem? Still, that code was so ancient as to need to be buried.

KiwiGuy
11-22-2012, 01:18 PM
That worked well, thanks!

I did have the relevant pages uploaded with the old script but it wasn't working. Now that you mention that the script is prehistoric it makes sense!

It's funny, I'm pretty savvy with Actionscript and I couldn't figure out what the problem with the Javascript was.

Anyway, thanks again for your help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum