Executing JS for iframe [duplicate]
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!
if you are in same domain you can use window.parent
inside your iframe to load an script and your desired elements and using eval
ing 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);
Use .contents()
.
$('iframe').contents().find('#test').remove();
精彩评论