How change a iframe content? [duplicate]

2019-09-04 17:04发布

Possible Duplicate:
How to change style of iframe content cross-domain?

Hi everybody.

Is it possible to change an iframe content if it comes from another site? e.g.

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

Thanks.

5条回答
Juvenile、少年°
2楼-- · 2019-09-04 17:14

No, but you can communicate if both pages agree to do so, such as with window.postMessage. This is a security feature for your protection. Here's a link to a jquery plugin window.postMessage wrapper with location.hash fallback support.

查看更多
三岁会撩人
3楼-- · 2019-09-04 17:27

Simple answer... no. If your referring to dom manipulation of the document loaded in the iframe.

For a longer answer see http://en.wikipedia.org/wiki/Cross-site_scripting

查看更多
手持菜刀,她持情操
4楼-- · 2019-09-04 17:32

No;

But you can get around this, sort of, if you pipe the content through your own domain.

So if you have a script at mydomain/fakegoogle that does something along the lines of

echo file_get_contents("google.com").

Then on your iframe, point the source at mydomain/fakegoogle. Not the most efficient, but it works if all you need is the content from that cross domain request.

查看更多
成全新的幸福
5楼-- · 2019-09-04 17:33

Not what you're going to want to hear but... No... You can't even use JavaScript to alter the contents of the iFrame if you do not have control over the domain and can access the files in your example Google.com.

查看更多
Viruses.
6楼-- · 2019-09-04 17:36

No it is not possible to edit the contents of an iframe from another domain, otherwise this would be a huge security risk.

This is called Cross Site Scripting, which is always a no-no.

查看更多
登录 后发表回答