views:

103

answers:

3

Hi, i was wondering if i can add/append a javascript to an iframe which is loaded with an external web page that is not in my website. is it possible using jQuery or even simple javascript ?

  <script>
  // i need some code here
  </script>
  <iframe src="http://www.xxx.xxx/" id="myiframe" />

Note: i don't want to replace the content of the iframe, i just want to add the script

Thanks

+5  A: 

If the iframe involves content from a different domain, the browser is not going to let your code poke around in it.

Pointy
There's a runaround for this which is called iframe proxy. But its a little hard to use.
Aykut
@Aykut: the proxy would have to be on the hosting page's domain, and you would have to add the `<base>` tag to the document but it's a hacky solution and you'd still have problems.
Andy E
+10  A: 

No, it's not possible (and quite rightly too) because it violates the same origin policy. It would be rather a large security hole to allow users to script content that's on another domain, anything would be possible from keyhooking to stealing personal data.

Andy E
A: 

It's not possible due to security policy.

However, if you really want to do it, and have understand the security issues, you could give a try to a solution like Ajax Cross domain

Boris Guéry
I can't see how ACD can help here. He's including the content in an iframe and ACD is just a work around for cross domain requests. It doesn't in any way help in accessing the DOM of a different origin document.
Andy E