Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Random iframe

  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Location
    Napier, New Zealand
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Random iframe

    Hi

    I got this script online which is meant to display random iframes onload. This script works for me locally, however when uploading and testing online it does not work. It displays absolutely nothing. I tested this with Firefox and IE.

    I'm an amateur with Javascript so if someone could spot why this is happening that would be awesome!

    Thanks

    <script language="JavaScript1.2">

    var ie=document.all&&navigator.userAgent.indexOf("Opera")==-1
    var dom=document.getElementById&&navigator.userAgent.indexOf("Opera")==-1

    var iframeprops='width=130 height=130 marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="1" scrolling="no"'

    var randomcontent=new Array()
    randomcontent[0]="random1.htm"
    randomcontent[1]="random2.htm"
    randomcontent[2]="random3.htm"
    randomcontent[3]="random4.htm"

    if (ie||dom)
    document.write('<iframe id="dynstuff" src="" '+iframeprops+'></iframe>')

    function random_iframe(){
    if (ie||dom){
    var iframeobj=document.getElementById? document.getElementById("dynstuff") : document.all.dynstuff
    iframeobj.src=randomcontent[Math.floor(Math.random()*randomcontent.length)]
    }
    }

    window.onload=random_iframe
    </script>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,232
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    Um...that script is ancient history.

    It's pretty much as obsolete as you can get.

    Let's rewrite it...

    Code:
    <!DOCTYPE html>
    <html>
    <body>
    Here is some stuff not in the iframe.<br/><br/>
    Here is some stuff not in the iframe.<br/><br/>
    Here is some stuff not in the iframe.<br/><br/>
    Here is some stuff not in the iframe.<br/><br/>
    
    <iframe id="dynstuff" width="100%" height="130" marginwidth="0" marginheight="0" 
            hspace="0" vspace="0" frameborder="1" scrolling="no">
    </iframe>
    
    <script type="text/javascript">
    (
      function( )
      {
          var randomcontent = [
               "http://www.codingforums.com",
               "http://www.espn.com",
               "http://www.google.com",
               "http://www.cnn.com" 
          ];
    
          var theFrame = document.getElementById("dynstuff");
    
          var which = Math.floor( Math.random() * randomcontent.length );
    
          theFrame.src = randomcontent[which];  
      }
    )();
    </script>
    </body>
    </html>
    I did just realize that if you didn't upload all those "randomX.html" pages to your online site the code would never have worked. Could that have been the problem? Still, that code was so ancient as to need to be buried.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Nov 2012
    Location
    Napier, New Zealand
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That worked well, thanks!

    I did have the relevant pages uploaded with the old script but it wasn't working. Now that you mention that the script is prehistoric it makes sense!

    It's funny, I'm pretty savvy with Actionscript and I couldn't figure out what the problem with the Javascript was.

    Anyway, thanks again for your help.


  •  

    Posting Permissions

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