Results 1 to 8 of 8

Thread: random url in iframe with timer?

  1. #1
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post random url in iframe with timer?

    I am using the following script in my header tag to display rotated urls in an iframe but I would like this to be random rather than in a particular order but also retaining the ability to have urls rotated every number of seconds or so like my current script does. If someone could help suggest any modification to the below code to do this then it would be greatly helpful. Thanks.

    <script type="text/javascript">


    var pages=new Array();
    pages[0]="http://www.link1.com";
    pages[1]="http://www.link2.com";
    pages[2]="http://www.link3.com";
    pages[3]="http://www.link4.com";

    var i=0;
    var time=7000; // this is set in milliseconds

    function pageChange() {
    document.getElementById("frame").src=pages[i];
    i++;
    if(i==pages.length) {
    i=0;
    }
    setTimeout("pageChange()",time);
    }
    onload=pageChange;


    </script>

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    See if this helps:
    Code:
    <script type="text/javascript">
    var pages=new Array();
    pages[0]="http://www.link1.com";
    pages[1]="http://www.link2.com";
    pages[2]="http://www.link3.com";
    pages[3]="http://www.link4.com";
    var time=7000; // this is set in milliseconds
    function pageChange() {
    var rand=Math.floor(Math.random()*pages.length);
    document.getElementById("frame").src=pages[rand];
    setTimeout("pageChange()",time);
    }
    onload=pageChange;
    </script>
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. #3
    Join Date
    Jun 2008
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Can anybody suggests how to do above per hour...like evry hour at starting the page changes and remains there till next hour.....

    P.S. I don't want to get it changed on every reload.....

    Can anybody help me on this??

    Regards
    pankajt

  4. #4
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by pankajt View Post
    Can anybody suggests how to do above per hour...like evry hour at starting the page changes and remains there till next hour.....

    P.S. I don't want to get it changed on every reload.....

    Can anybody help me on this??

    Regards
    pankajt
    I think you need something like PHP to do that.

    Thanks for the above script! It works perfectly =p

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,133
    Thanks
    44
    Thanked 3,231 Times in 3,192 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by pankajt View Post
    Can anybody suggests how to do above per hour...like evry hour at starting the page changes and remains there till next hour.....

    P.S. I don't want to get it changed on every reload.....

    Can anybody help me on this??

    Regards
    pankajt

    If you have exactly 24 pages:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    </head>
    <body>
    <iframe name="hourFrame" src="about:blank" width="300" height="300" scrolling="auto" frameborder="1"></iframe>
    <script type="text/javascript" defer="defer">
    ;(function(){
    if ( !window.frames || !window.location.replace ) return;
    var pages = [];
     pages[0] = 'http://www.link1.com';
     pages[1] = 'http://www.link2.com';
     pages[2] = 'http://www.link3.com';
     pages[3] = 'http://www.link4.com';
     pages[4] = 'http://www.link5.com';
     pages[5] = 'http://www.link6.com';
     pages[6] = 'http://www.link7.com';
     pages[7] = 'http://www.link8.com';
     pages[8] = 'http://www.link9.com';
     pages[9] = 'http://www.link10.com';
    pages[10] = 'http://www.link11.com';
    pages[11] = 'http://www.link12.com';
    pages[12] = 'http://www.link13.com';
    pages[13] = 'http://www.link14.com';
    pages[14] = 'http://www.link15.com';
    pages[15] = 'http://www.link16.com';
    pages[16] = 'http://www.link17.com';
    pages[17] = 'http://www.link18.com';
    pages[18] = 'http://www.link19.com';
    pages[19] = 'http://www.link20.com';
    pages[20] = 'http://www.link21.com';
    pages[21] = 'http://www.link22.com';
    pages[22] = 'http://www.link23.com';
    pages[23] = 'http://www.link24.com';
    window.frames['hourFrame'].location.replace( pages[new Date().getUTCHours()] );
    })();
    </script>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,133
    Thanks
    44
    Thanked 3,231 Times in 3,192 Posts
    Blog Entries
    12

    Default

    Or, for virtually any number of pages, one page per hour:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    </head>
    <body>
    <iframe name="hourFrame" src="about:blank" width="300" height="300" scrolling="auto" frameborder="1"></iframe>
    <script type="text/javascript" defer="defer">
    ;(function(){
    if ( !window.frames || !window.location.replace ) return;
    var pages = [];
     pages[0] = 'http://www.link1.com';
     pages[1] = 'http://www.link2.com';
     pages[2] = 'http://www.link3.com';
     pages[3] = 'http://www.link4.com';
     pages[4] = 'http://www.link5.com';
     pages[5] = 'http://www.link6.com';
     pages[6] = 'http://www.link7.com';
    window.frames['hourFrame'].location.replace( pages[Math.floor(new Date().valueOf()/(1000*60*60))%pages.length] );
    })();
    </script>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Oct 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi folks sorry for resurrecting such an old post.
    I've trawled the internet for a long while before I found this solution.

    I have used it and it is great but I do have a question.
    Would it possible that the timed event was less random.
    For instance I have used it for say 2 pages but I want 1 to be visible 80% of the time and the other 20%
    Is this a realistic task?

    Many thanks great site I think I will be spending more time here.

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,133
    Thanks
    44
    Thanked 3,231 Times in 3,192 Posts
    Blog Entries
    12

    Default

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    </head>
    <body>
    <iframe name="hourFrame" src="about:blank" width="300" height="300" scrolling="auto" frameborder="1"></iframe>
    <script type="text/javascript" defer="defer">
    ;(function(){
    if ( !window.frames || !window.location.replace ) return;
    var pages = [];
     pages[0] = 'http://www.link1.com';
     pages[1] = 'http://www.dynamicdrive.com';
    var rand = Math.random() * 100;
    rand = rand < 20? 0 : 1;
    window.frames['hourFrame'].location.replace( pages[rand] );
    var sf = arguments.callee;
    setTimeout(function(){sf();}, 5000); // get rid of this line if you don't want it looping (5000) is the number of milliseconds per loop
    })();
    </script>
    </body>
    </html>
    Last edited by jscheuer1; 10-23-2014 at 03:43 PM. Reason: add loop, change second url to one that will load
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •