Solved

How to load random html file in iframe?

Posted on 2003-11-06
15
580 Views
Last Modified: 2012-06-22
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?
0
Comment
Question by:mikezang
  • 10
  • 5
15 Comments
 
LVL 1

Expert Comment

by:Jagata
ID: 9698517
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
 
LVL 1

Expert Comment

by:Jagata
ID: 9698519
Here some additional information on the JavaScript math object; http://www.devguru.com/Technologies/ecmascript/quickref/math.html
0
 
LVL 5

Author Comment

by:mikezang
ID: 9698721
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
 
LVL 5

Author Comment

by:mikezang
ID: 9736354
I will delete this question.
0
 
LVL 1

Accepted Solution

by:
Jagata earned 125 total points
ID: 9736386
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
 
LVL 1

Expert Comment

by:Jagata
ID: 9736583
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
 
LVL 5

Author Comment

by:mikezang
ID: 9736616
Thanks.
How can I use pages collection?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:Jagata
ID: 9736677
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
 
LVL 1

Expert Comment

by:Jagata
ID: 9736702
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
 
LVL 5

Author Comment

by:mikezang
ID: 9736710
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
 
LVL 1

Expert Comment

by:Jagata
ID: 9736713
Buggar the above line;
    Response.Write "arrPageCollection(" & pintIndex & ") = '" & rstPageCollection("fldHREF") & "';" & vbCrLf

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


Sorry! :)
0
 
LVL 1

Expert Comment

by:Jagata
ID: 9736893
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
 
LVL 1

Expert Comment

by:Jagata
ID: 9736932
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
 
LVL 1

Expert Comment

by:Jagata
ID: 9736933
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
 
LVL 5

Author Comment

by:mikezang
ID: 9736949
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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now