开发者

Executing JS for iframe [duplicate]

开发者 https://www.devze.com 2023-04-08 22:35 出处:网络
This question alread开发者_运维技巧y has answers here: Closed 11 years ago. Possible Duplicate: Invoking javascript in iframe from parent page
This question alread开发者_运维技巧y has answers here: Closed 11 years ago.

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 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);


Use .contents().

$('iframe').contents().find('#test').remove();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号