1

Possible Duplicate:
Invoking javascript in iframe from parent page

what Im trying to do is - load a html page with iframe'd website, and then remove an element of that website, lets call it '#test'

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://gmpg.org/xfn/11">

</head>
<body>
    <iframe src="http://www.google.com"></iframe>
</body>
</html>

Anyone got any ideas how I would go about this? Can't seem to do it in normal .remove() fashion...

Thanks!

Community
  • 1
  • 1
Saulius Antanavicius
  • 1,371
  • 6
  • 25
  • 55
  • [Invoking javascript in iframe from parent page](http://stackoverflow.com/questions/251420/invoking-javascript-in-iframe-from-parent-page) – rlemon Sep 28 '11 at 23:02
  • From your example: read the answer posted by [VCS](http://stackoverflow.com/questions/251420/invoking-javascript-in-iframe-from-parent-page/2663118#2663118) – rlemon Sep 28 '11 at 23:04
  • I assume the `iframe` and page you're loading the iframe in are on different domains? In this case, there's very little you can do thanks to the cross domain browser policy, which prevents pages on one domain executed JS on another domain. – Alex Sep 28 '11 at 23:28

2 Answers2

4

if you are in same domain you can use window.parent inside your iframe to load an script and your desired elements and using evaling that script remove the iframe itself

code for inner window aka the iframe:

var myscript = document.createElement('script');
myscript.innerHTML = "var removeIframe = function(){document.body.removeChild(document.body.querySelector('iframe'));}"
window.parent.document.body.appendChild(myscript);
window.parent.eval(removeIframe);
Mohsen
  • 64,437
  • 34
  • 159
  • 186
1

Use .contents().

$('iframe').contents().find('#test').remove();
Alex
  • 9,313
  • 1
  • 39
  • 44
  • Permission denied to access property 'ownerDocument' [Break On This Error] (function(a,b){function cg(a){return d...a:a+"px")}}),a.jQuery=a.$=d})(window); – Saulius Antanavicius Sep 28 '11 at 23:26