Link to home
Start Free TrialLog in
Avatar of mikezang
mikezang

asked on

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?
Avatar of Jagata
Jagata

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.
Here some additional information on the JavaScript math object; http://www.devguru.com/Technologies/ecmascript/quickref/math.html
Avatar of mikezang

ASKER

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.
I will delete this question.
ASKER CERTIFIED SOLUTION
Avatar of Jagata
Jagata

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Thanks.
How can I use pages collection?
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>
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!
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?
Buggar the above line;
    Response.Write "arrPageCollection(" & pintIndex & ") = '" & rstPageCollection("fldHREF") & "';" & vbCrLf

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


Sorry! :)
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.
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.
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.
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.