How to load random html file in iframe?

I have a main page that include a float iframe with a html file, there is two iframes in that html file, I hope they will load random html file when I scroll the main page.
What can I do?
LVL 5
mikezangAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JagataCommented:
Just use this little script. :)

<SCRIPT LANGUAGE = "JavaScript">
   <!--

      var arrPageCollection = new Array(2)
      arrPageCollection[0] = "http://www.yahoo.com";
      arrPageCollection[1] = "http://www.google.com";
      arrPageCollection[2] = "http://www.firingsquad.com";

      var intRandomIndex = Math.round(Math.random() * (arrPageCollection.length - 1));
      document.location.href = arrPageCollection[intRandomIndex];

   //-->
</SCRIPT>

Hope this helps.

Jason.
0
JagataCommented:
Here some additional information on the JavaScript math object; http://www.devguru.com/Technologies/ecmascript/quickref/math.html
0
mikezangAuthor Commented:
Thanks for your suggestions.
Actually, I had gotten the answer by myself.
In main page:
<body OnScroll="location.reload();">
...
In the first iframe page:
<Script>
function load() {
  var n = Math.floor(Math.random() * 100);
  document.all.myiframe.src = n + ".html";
}
</Script>
<body OnLoad="load();">
<iframe src=0.html></iframe>
...

You can view my page at http://www.geocities.jp/mikezang/poetry after 10 hours.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

mikezangAuthor Commented:
I will delete this question.
0
JagataCommented:
Good work Mike, glad I put in an effort to help. Please keep your question up to date so I don't have to waste my time.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JagataCommented:
Thankyou Mike. Just a little something extra for you.

Instead of your current solution you could just put this in the IFRAME in order to achieve a greater simplicity;

<SCRIPT LANGUAGE = "JavaScript">
   <!--
      var n = Math.floor(Math.random() * 100);
      document.location.href = n + ".html";
   //-->
</SCRIPT>

That above will do exactly the same thing. I think however that you would be better using a pages collection because it will make it much easier to reference your pages, and when you add additional pages you just need to update the array. You could also populate the array from a database which is another benefit.
0
mikezangAuthor Commented:
Thanks.
How can I use pages collection?
0
JagataCommented:
The pages collection is the array below. So if you wanted to add a new entry, you would just add it below the last item and increment the index by 1. For example you could add the following;

   arrPageCollection[3] = "http://www.geocities.jp/mikezang/poetry";

That is all you need to do, you do not need to modify any other part of the code which makes it very easier to implement, and you can also use much friendlier names for your pages. :)

<SCRIPT LANGUAGE = "JavaScript">
   <!--

      var arrPageCollection = new Array()
      // *** User Created Pages Collection ***
      arrPageCollection[0] = "http://www.yahoo.com";
      arrPageCollection[1] = "http://www.google.com";
      arrPageCollection[2] = "http://www.firingsquad.com";

      // *** Select Random Page from Collection ***
      var intRandomIndex = Math.round(Math.random() * (arrPageCollection.length - 1));
      // *** Load Random Page
      document.location.href = arrPageCollection[intRandomIndex];

   //-->
</SCRIPT>
0
JagataCommented:
Okay so next so your an ASP programmer (not sure) and you wanted to load the pages collection from a database. You could implement the following;

<SCRIPT LANGUAGE = "JavaScript">
   <!--

      var arrPageCollection = new Array()
      // *** User Created Pages Collection ***
      <%
      Dim pintIndex
      For pintIndex = 0 To rstPageCollection.RecordCount - 1
         Response.Write "arrPageCollection(" & pintIndex & ") = '" & rstPageCollection("fldHREF") & "';" & vbCrLf
         rstPageCollection.MoveNext
      Next
      %>

      // *** Select Random Page from Collection ***
      var intRandomIndex = Math.round(Math.random() * (arrPageCollection.length - 1));
      // *** Load Random Page
      document.location.href = arrPageCollection[intRandomIndex];

   //-->
</SCRIPT>

So the above code loops through all pages in the database, and then dynamically creates the JavaScript for the page collection for you!
0
mikezangAuthor Commented:
Thanks again.

I think it is difficult because I have thousands of html file, then the file name is in order number format as 000001.html ... 999999.html, maybe my method is better, do you think so?
0
JagataCommented:
Buggar the above line;
    Response.Write "arrPageCollection(" & pintIndex & ") = '" & rstPageCollection("fldHREF") & "';" & vbCrLf

Should be;
   Response.Write "arrPageCollection[" & pintIndex & "] = '" & rstPageCollection("fldHREF") & "';" & vbCrLf


Sorry! :)
0
JagataCommented:
In that case, you are right it would be better to use a numeric format for the pages. However your solution will not work. For the following reasons;

   1. The format 000001.html ... 999999.html, has leading zero's which your solution will not accommodate. Therefore when 'n' = 1 your
       solution will load 1.html and not 000001.html.

   2. var n = Math.floor(Math.random() * 100); will return values from 0 to 99. Since you don't have a page 0.html your solution will return
       a page cannot be found error.

   3. Your solution will never display any pages above 99, i.e. all pages from 100 to 999999 will never be display.

I have prepared another solution using your methodology, let me know if you have any questions regarding the code.

<SCRIPT LANGUAGE = "JavaScript">
   <!--

      function load() {
         // *** Specify Page Count as intPageCount ***
         var intPageCount = 999999;
         // *** Do Not Modify ***
         var intPageLength = new String(intPageCount).length;
         intPageCount = intPageCount - 1;
         var intPageNumber = Math.round(Math.random() * intPageCount) + 1;
         var strPageName = new String(intPageNumber);
         for (var intIndex = 0; intIndex < intPageLength; intIndex++) {
            strPageName = "0" + strPageName;
         }
         document.location.href = strPageName.substring(strPageName.length - intPageLength, strPageName.length) + ".html";
      }

   //-->
</SCRIPT>

With the above solution, all you need do is specify the maximum page number (intPageCount). So if the maximum page was 000198.html,
intPageCount would equal 198. No other code need be modified.

I hope this helps.
0
JagataCommented:
Actually a slightly better solution would be;

<SCRIPT LANGUAGE = "JavaScript">
   <!--

      function load() {
         // *** Specify Page Count as intPageCount ***
         var intPageCount = 999999;
         var intPageLength = 6;
         // *** Do Not Modify ***
         intPageCount = intPageCount - 1;
         var intPageNumber = Math.round(Math.random() * intPageCount) + 1;
         var strPageName = new String(intPageNumber);
         for (var intIndex = 0; intIndex < intPageLength; intIndex++) {
            strPageName = "0" + strPageName;
         }
         document.location.href = strPageName.substring(strPageName.length - intPageLength, strPageName.length) + ".html";
      }

   //-->
</SCRIPT>

This method also allows you to specify the page length; therefore if the maximum page number is 198, and the page is 000198.html
intPageCount = 198 & intPageLength = 6, that will accommodate the leading zeros, whereas the first solution will not.
0
JagataCommented:
Actually a slightly better solution would be;

<SCRIPT LANGUAGE = "JavaScript">
   <!--

      function load() {
         // *** Specify Page Count as intPageCount ***
         var intPageCount = 999999;
         var intPageLength = 6;
         // *** Do Not Modify ***
         intPageCount = intPageCount - 1;
         var intPageNumber = Math.round(Math.random() * intPageCount) + 1;
         var strPageName = new String(intPageNumber);
         for (var intIndex = 0; intIndex < intPageLength; intIndex++) {
            strPageName = "0" + strPageName;
         }
         document.location.href = strPageName.substring(strPageName.length - intPageLength, strPageName.length) + ".html";
      }

   //-->
</SCRIPT>

This method also allows you to specify the page length; therefore if the maximum page number is 198, and the page is 000198.html
intPageCount = 198 & intPageLength = 6, that will accommodate the leading zeros, whereas the first solution will not.
0
mikezangAuthor Commented:
Actually, I used this method
n = Math.floor(Math.random() * 100) + 1;
n += "";
var zero = "00000000";
var len = n.length;
var m = zero.substring(0, l - len) + n + ".html";

Anyway, thanks for your answer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.