Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Thanked 0 Times in 0 Posts

    Question Chaning Iframe source on button click

    I have a script which creates an element of iframe. I have a button which refreshes the iframe link.

    I need to do something to change a variable in the iframe src on every click of the button.

    I'll explain the real issue. I have a list of games I have with random name like,

    boomer.swf, kingdom.swf, balloon.swf and so on.

    So I somehow made this,

    var r_text = new Array ();
    r_text[0] = "boomer";
    r_text[1] = "kingdom";
    r_text[2] = "balloon";
    var i = Math.floor(3*Math.random())
    var gamedrome = { game: ""+r_text[i]+""};
    Now I have a .js file which creates iframe element
    function embed_game(params, options)
    var data = [];
    for(var i in params)
    data.push(i + "=" + encodeURIComponent(params[i]));
    data = data.join("&");
    var frame = document.createElement("iframe");
    frame.src = "http://website.com/embed/game.html?"+ data;
    frame.style.border = "0";
    frame.frameBorder = "0";
    and another small script to refresh the inner iframe and not the page to change game.

    <script type="text/javascript">
    function Reload () {
    var f = document.getElementById('newroom');
    f.src = f.src;
    It all works fine until the game is loaded, but the problem is that if once the game is loaded in site, the iframe source is already set and my button keeps loading the same game. I need to randomize it on every click. Please help me out.

  2. #2
    Regular Coder
    Join Date
    Sep 2011
    United Kingdom
    Thanked 4 Times in 4 Posts
    Hi, I don't know the solution but I would make it a lot simpler.

    I would save each of your .swf files in a html file. So you would be able to load them seperately in a individual page.

    Than I would save the url to each or this files in a Array.
    In this case something like:
    frame.src = "http://website.com/embed/"+r_text+"html";
    If you do it my way, the Array pointer should move down one position each time you trigger the function by pressing the change game button. Because that is what pointers do. No random number needed.

    I had some problem to get my head around your code. For example:
    f.src = f.src;
    Stays the same ?

    var i = Math.floor(3*Math.random())
    Semicolon missing at the end and you never get to 3 with this only 0 until 2. Need to add +1 (I guess).

    Hope it was of some help.
    Last edited by clausrei; 02-23-2012 at 08:43 AM.


Tags for this Thread

Posting Permissions

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