Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-22-2012, 04:26 AM   PM User | #1
lotterys
New to the CF scene

 
Join Date: Feb 2012
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
lotterys is an unknown quantity at this point
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.
lotterys is offline   Reply With Quote
Old 02-23-2012, 07:39 AM   PM User | #2
clausrei
New Coder

 
Join Date: Sep 2011
Location: United Kingdom
Posts: 72
Thanks: 2
Thanked 3 Times in 3 Posts
clausrei is an unknown quantity at this point
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..
clausrei is offline   Reply With Quote
Reply

Bookmarks

Tags
button, click, iframe, source

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:32 PM.


Advertisement
Log in to turn off these ads.