PDA

View Full Version : populate an iframe with random page



bcbasslet
Sep 1st, 2003, 07:29 AM
<script>
var randomnumber=Math.floor(Math.random()*55)
var pnum = "page"+randomnumber+".htm"
</script>
</head>

<iframe name="fred"
<script language="JavaScript">
document.write(' src="mypage' + pnum + '.htm" > ');
</script></iframe>

i'm trying to populate an iframe with a randomly selected page.
I just don't know enuf syntax to make this work.
Could someone help me, please?

lavalamp
Sep 1st, 2003, 07:23 PM
Since you are creating the src attribute for the iframe with js, it won't work for those without js, so you have two options open to you:

1 Create the whole iframe with js and have a noscript alternative:

<script type="text/javascript"><!--

document.write('<iframe name="fred" src="mypage' + pnum + '.htm">You cannot view iframes.</iframe>');

//--></script>

<noscript>

<iframe name="fred" src="default.htm">You cannot view iframes.</iframe>

</noscript>

2 You create the iframe as normal and have a redirect to a random page built into it, therefore it won't work for non-js browsers but at least they can still see an iframe with something in it.


If you were to use server side code you could create an iframe to a random page and then even non-js browsers could join in the fun.

bcbasslet
Sep 2nd, 2003, 04:39 PM
thanks so much, but i tried that.
and it posts the frame, but doesn't populate it.
i'm in an intranet env. so everyone has ie6 and js.
(too easy, right).

fredmv
Sep 2nd, 2003, 04:43 PM
Hey, bcbasslet. Try this:



<script type="text/javascript">
var urls = new Array("http://www.yahoo.com/", "http://www.google.com/", "http://www.hotbot.com/");
document.writeln("<iframe src='" + urls[Math.floor(Math.random()*urls.length)] + "' style='height: 300px; width: 550px;'><\/iframe>");
</script>


Hope that helps you out.

bcbasslet
Sep 2nd, 2003, 06:12 PM
thanks fred, i got that to work.
I had hoped to avoid placing the filenames into an array
since i will have about 200 of them.
I had hoped to be able to do it with a reference.

I had not seen the command document.writeln
i had only seen document.write
Can you tell me what the "ln" does, or is?
thanks fred.

bcbasslet
Sep 2nd, 2003, 06:16 PM
ah! i figured it out! bless you Fred.
<script>
var randomnumber=Math.floor(Math.random()*3) + 1
var pnum = "page"+randomnumber+".htm"
</script>
</head>

and then...

document.writeln("<iframe src='http://inseam/reports/frametest" + randomnumber + ".htm' style='height: 300px; width: 550px;'><\/iframe>");

Its that writeln command i didn't know. thanks so much!

fredmv
Sep 2nd, 2003, 06:28 PM
You're welcome. :D

The writeln method does the exact same thing as the write method except it appends a newline (\n) character to the end of the output. This is only noticable when you look at the output in plaintext.

You could also shorten your script to this:



<script type="text/javascript">
document.writeln("<iframe src='http://inseam/reports/frametest/page" + Math.floor(Math.random()*3) + ".htm' style='height: 300px; width: 550px;'><\/iframe>");
</script>