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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    2
    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,

    Code:
    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
    Code:
    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.width="100%";
    frame.style.height="100%";
    frame.style.border = "0";
    frame.frameBorder = "0";
    frame.id="newroom";
    ....
    and another small script to refresh the inner iframe and not the page to change game.

    Code:
    <script type="text/javascript">
    function Reload () {
    var f = document.getElementById('newroom');
    f.src = f.src;
    }
    </script>
    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
    New Coder
    Join Date
    Sep 2011
    Location
    United Kingdom
    Posts
    74
    Thanks
    2
    Thanked 3 Times in 3 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:
    Code:
    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:
    Code:
    f.src = f.src;
    Stays the same ?

    Code:
    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 07: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
    •