Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fire function in iframe page from outside the iframe?

Status
Not open for further replies.

cbsarge

IS-IT--Management
Jun 20, 2001
219
US
I am trying to build a page that is basically a toolbar to use with an online game. The game is all run in a webpage and all my toolbar will be doing is giving quicker/easier access to things already in the game. In this scenario there are consumable objects in the game that are used by the function:

Code:
ACTIONS.use_consumable(1,11)

It is a bit laborious to get to the screen where you use these items so my idea is to create a page with these functions assigned to some buttons and then have the actual game page load in an iframe taking up the entire browser except a sliver at the top where my buttons will be.

Is it possible to do this? If I name the iframe foo could I use something like:

Code:
<input type="button" value="Use 1" target="foo" onclick="ACTIONS.use_consumable(1,11)">

Thank you for any help given.
 
The essence of what you want to do is possible, however several factors come into play that would determine one way or another.

[ol]
[li]The game/page/code that runs in the ifrmae must be located in the same domain as the code outside the iframe for it to be able to access the iframe contents. If say your toolbar page is in but the game is in then js will not be able to access anything from the game.com domain and so your toolbar is useless. [/li]

[li]There is no target property in a button. to target code inside an ifrmae you need to do it through frames collection, or via the frames name or ID. as:

onclick="window.frames.framename_or_id.functionname();"

[/li]
[li]Access will also be dependant on whether the game's functions are public, if they are set as private for an object in the game's code, then only the object can access the functions. [/li]
[/ol]

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Hi

Phil said:
There is no target property in a button.
Correct.

Just a small note, mostly for the future visitors of this thread : HTML5 provides an easier way :
HTML5 differences from HTML4 said:
The [tt]input[/tt] and [tt]button[/tt] elements have [tt]formaction[/tt], [tt]formenctype[/tt], [tt]formmethod[/tt], [tt]formnovalidate[/tt], and [tt]formtarget[/tt] as new attributes. If present, they override the [tt]action[/tt], [tt]enctype[/tt], [tt]method[/tt], [tt]novalidate[/tt], and [tt]target[/tt] attributes on the [tt]form[/tt] element.
( HTML5 differences from HTML4 | Language | New Attributes )

[tt]formtarget[/tt] certainly works in the latest Gecko, Presto and WebKit. No idea since when.


Feherke.
 
The javascript from the game page is visible in the source of the page. I can put the same javascript in my page and then fire the function at the iframe but, this is not ideal as it would require me to update my toolbar any time the game changes.

What about a Chrome extension to replace some element of the rendered game page with code including the code that makes up my tool bar? Would the game page then interpret it as being in the same domain?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top