How change a iframe content? [duplicate]

2019-09-04 17:00发布

问题:

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.

回答1:

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.



回答2:

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



回答3:

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.



回答4:

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.



回答5:

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.