1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

Javascript Question

Discussion in 'BBS Hangout' started by Major, May 1, 2004.

Tags:
  1. Major

    Major Member

    Joined:
    Jun 28, 1999
    Messages:
    41,688
    Likes Received:
    16,224
    Hello computer programming types... I have a Javascript question. I created a short javascript program at:

    http://ads.swirve.com/test/z.htm

    It is supposed to select from one of 3 random frame sizes and then load up the proper frame in it. The code is very, very simple:

    <I>
    var id = 1 + Math.floor(Math.random() * 3);

    if (id==1) { document.write('Frame Style 1 ifr'+'ame src="http://ads.swirve.com/test/frame1.htm" width=470 height=62 frameborder=0 marginheight=0 marginwidth=0 scrolling=no>/ifr'+'ame>'); }

    if (id==2) { document.write('Frame Style 2 ifr'+'ame src="http://ads.swirve.com/test/frame2.htm" width=730 height=92 frameborder=0 marginheight=0 marginwidth=0 scrolling=no> /ifr'+'ame>'); }

    if (id==3) { document.write('Frame Style 3 ifr'+'ame src="http://ads.swirve.com/test/frame3.htm" width=302 height=252 frameborder=0 marginheight=0 marginwidth=0 scrolling=no> /ifr'+'ame>'); }
    </I>

    (note: I took out the bracket before the IFRAME and /IFRAME tags above to make it show properly here - the code should be correct in the actual page and you can view source to see the actual code).

    Essentially, it picks a random number 1-3, and then displays the proper IFRAME. Now, each of the three "frame#.htm" files just contains basic text that says "This is frame #1 - Designed for 468x60" (or #2 / 728x90 or #3 / 300x250).

    So, one of three things should happen:

    A 468x60 frame should show up with the text "This is Frame #1".
    A 728x90 frame should show up with the text "This is Frame #2".
    A 300x250 frame should show up with the text "This is Frame #3".

    Now, go to the link and keep hitting reload - this is NOT what happens. You'll get different frame sizes, but it seems the same iframe keeps loading on the inside. For example, you'll get a frame 468x60 that says "this is frame #3, 300x250".

    Generally, whatever loads the first time you run the program will continue to load in the future, even though the random number changes and the frame size changes properly.

    Anyone have any ideas what might cause this? Thank you for any help!
     
  2. Major

    Major Member

    Joined:
    Jun 28, 1999
    Messages:
    41,688
    Likes Received:
    16,224
    I also had thought it might have something to do with the iframes not having a name, but I tried adding a random name each time, and that didn't solve the problem either.
     
  3. DanzelKun

    DanzelKun Member

    Joined:
    Nov 16, 2002
    Messages:
    1,218
    Likes Received:
    6
    Lesse, I'm not positive if it's the same with Javascript, but I know with C the random number generator isn't actually random, just a really complex equation that will give you the same result each time you run your program UNLESS you change the Seed. The seed is the base number send to the random function that determines the outcome of the random function.

    Often times in C, people will Seed the rand function with the time function call because it changes every millisecond and you are guaranteed a different result from your random number generator every millisecond.

    That would be my best guess, again I've never actually programmed in java, but I hear that it and C have some ties.
     
  4. Major

    Major Member

    Joined:
    Jun 28, 1999
    Messages:
    41,688
    Likes Received:
    16,224
    Lesse, I'm not positive if it's the same with Javascript, but I know with C the random number generator isn't actually random, just a really complex equation that will give you the same result each time you run your program UNLESS you change the Seed. The seed is the base number send to the random function that determines the outcome of the random function.


    Yeah, that was my first thought too -- but size & source of the frame are linked. That is, if the random number is 1, it should create a frame of the size 468x60 with the source being FRAME1.HTM. If it's 2, it will create a frame 728x90 with the source being FRAME2.HTM. If it's 3, it'll create a frame 300x250 with the source being FRAME3.HTM.

    So even if the random number generator is not functioning properly, that would just result in one of the three options showing up too much or whatever. But I'm getting mixed results - that is, a 468x60 frame coming up with FRAME2.HTM or FRAME3.HTM, which should be impossible.

    Thanks though!
     
  5. keeley

    keeley Member

    Joined:
    Feb 13, 1999
    Messages:
    1,513
    Likes Received:
    35
    I've played with this a little bit.

    I can get the different pages to load but in the same size iFrame, and I can get different sized iFrames to load but with the same page appearing in the iframe. I am using window.alert to show what document.write is writing to your page and that will show you that what you are actually writing to the page isn't what is loading into your window. Why, I have no idea. I tried to get rid of caching using meta tags but that isn't doing the trick.

    I'd just use server-side script to do this.
     
  6. DrLudicrous

    DrLudicrous Member

    Joined:
    May 9, 2002
    Messages:
    3,936
    Likes Received:
    203
    That's very strange. Messed around with it some and it seems that if you add name and id attributes to the iframe tag it will work. Have no idea why it doesn't work without it though.

    &lt;iframe src="http://ads.swirve.com/test/frame1.htm" width=470 height=62 frameborder=0 marginheight=0 marginwidth=0 scrolling=no id="frame1" name="frame1"&gt;&lt;/iframe&gt;
     
  7. Major

    Major Member

    Joined:
    Jun 28, 1999
    Messages:
    41,688
    Likes Received:
    16,224
    Thanks for the suggestions, guys.

    DrL, I uploaded your version with the name and ID tags in there to:

    http://ads.swirve.com/test/x.htm

    And get the same problem.

    Keeley - thanks for the research. Like you, I have no idea what would cause this. I'm trying to avoid Server Side Scripting, because this frame will be coming from an old machine and it will be run about 10 million times a day - I don't think that thing can handle it. I was hoping to make everything pure HTML to make it all run quickly and so that particular file only had to load once per user session and then just be run from the cache over and over to create the right IFRAMES. I'll keep experimenting.

    Thanks again for the ideas!
     
  8. DrLudicrous

    DrLudicrous Member

    Joined:
    May 9, 2002
    Messages:
    3,936
    Likes Received:
    203
    My first post was after testing on my mac only, I checked on my windows machine and the problem was in fact still there. I researched it some and didn't find much on it but a few other people had similar problems but couldn't find a solution.

    It seems to be a problem with the page refreshing but the iFrame not refreshing, if you just click "Go" next to the address over and over again it works ok, but not if you press the refresh button. I came up with this script to solve the problem, basically it loads a temporary page then changes the location once it's loaded.

    &lt;script language=Javascript&gt;

    var id = 1 + Math.floor(Math.random() * 3);
    document.write('&lt;P>Frame Style ' + id + '&lt;P&gt;&lt;iframe src="tmp.htm" width=302 height=252 frameborder=0 marginheight=0 marginwidth=0 scrolling=no name=adFrame id=adFrame>&lt;/iframe&gt;');
    window.frames['adFrame'].location.href = 'frame' + id + '.htm';

    &lt;/script&gt;

    This one has been tested on IE5 and Netscape 6 on Windows, Safari and IE5 on Mac.
     
  9. DrLudicrous

    DrLudicrous Member

    Joined:
    May 9, 2002
    Messages:
    3,936
    Likes Received:
    203
     
  10. DrLudicrous

    DrLudicrous Member

    Joined:
    May 9, 2002
    Messages:
    3,936
    Likes Received:
    203
    I'm obisously not too good with this posting stuff, that last post had the addition to set the width and height according to which number was chosen.
     
  11. DanzelKun

    DanzelKun Member

    Joined:
    Nov 16, 2002
    Messages:
    1,218
    Likes Received:
    6
    Yeah, Major, going on what DrL said, I tried a "hard refresh" or whatever some people call it and did Ctrl+F5 to refresh rather than just F5. Try that because it worked for me with the inner frame changing correctly each time. So yeah it seems as though it's just got the old page loaded in your cache or whatever and refers to that instead of loading a new page.
     
  12. Major

    Major Member

    Joined:
    Jun 28, 1999
    Messages:
    41,688
    Likes Received:
    16,224
    GENIUS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    Thank you for that solution - it works perfectly!
     

Share This Page

  • About ClutchFans

    Since 1996, ClutchFans has been loud and proud covering the Houston Rockets, helping set an industry standard for team fan sites. The forums have been a home for Houston sports fans as well as basketball fanatics around the globe.

  • Support ClutchFans!

    If you find that ClutchFans is a valuable resource for you, please consider becoming a Supporting Member. Supporting Members can upload photos and attachments directly to their posts, customize their user title and more. Gold Supporters see zero ads!


    Upgrade Now