View Full Version : Adding javascript to a webpage inside a iframe?

12-06-2010, 04:26 PM
Alright so I've been teaching myself php/javascript/html/joomla for the past couple weeks, learning as i go along. So bear with me if i seem like a total moron =p.

I'm trying to run a script from a js file to add javascript to the website inside the iframe. I also would like the script to only be activated onclick(this is all in an article).

To clarify my website has an iframe and this iframe contains a webpage from http://website987.com/. I want to run thescript.js on the website inside that iframe, to add javascript to that website, however i also only want to run this js file on the click of a link/button. I hope i'm being clear if I'm not please tell me and ill try to reiterate.

I've been trying to get this to work for a while now and I feel like this is really simple and I'm asking a stupid question. However this is what I've got so far, currently this just redirects the iframe to the URL with my code on it(instead of actually running it).How could i edit this/what coding could I use to make it so thescript.js actually runs instead of it being just redirected?
Reply With Quote

$filename = 'thescript.js';
$path = '/media/system/js/'; // add the path parameter if the path is different than : 'media/system/js/'
JHTML::script($filename, $path, true); // MooTools will load if it is not already loaded


<script language="javascript">
function loadOne()
parent.FRAME1.location.javascript: function('/media/system/js/thescript.js')

<iframe name="FRAME1" src="http://website987.com/" width="740" height="500" frameborder="0" scrolling="yes"></iframe>

<a href="javascript:loadOne()">Click Here/a>

Am i even on the right track? I've been testing out a billion different codes and I think it would be ridiculous to post them all so I was hoping someone could help me fix this code or at least point me in the right direction. I'm usually able to figure everything out on my own but I'm failing atm and I just need help on this one thing, I would be ever so thankful for any assistance provided, !

(BTW I'm also currently using the Joomla CMS and a joomla plugin called CodeMirror that runs your code directly from the article, not sure if this is relevent or not)

Philip M
12-06-2010, 04:53 PM
For security reasons JavaScript cannot access anything beyond the domain of the current page. This is known as the "same origin policy" and prevents a document or script loaded from one origin from getting or setting properties of a document from a different origin.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

12-06-2010, 11:11 PM
Sorry maybe I should clarify, this script(thescript.js) simpley allows users to stream an avi file from a download service in a divx player. I have this download service in a iframe and am trying to run this script on the click of a button to activate the streaming. Are you saying that running this script on a iframe is not possible?

What if i instead ran the script data directly from my website instead of from an internal script would this change anything?

Sorry once again, im a newbie and thanks for responding.

Seeking intellectually weighted advice through volume often yields what your looking for. =)

Old Pedant
12-07-2010, 12:37 AM
You can change the ".src" of the <iframe>, from code in the page that contains it, but that is the *ONLY* thing you can change unless the domain that the <iframe> is loaded from is the same as the one the main page is loaded from.

So you could do

document.frames["FRAME1"].src = "http://www.website487.com/somepage.html?run=thescript.js";

And *IF* that page on that site understood that "command" it could then process your request to run that script.

But once the page from the "foreign" site is loaded into the iframe, your page has NO control on its contents. And you can't even communicate further with it.