iframe タグが親ドキュメントのコンテンツを囲んでいないため、iframe の innerHTML は空白です。iframe の src 属性によって参照されるページからコンテンツを取得するには、iframe の contentDocument プロパティにアクセスする必要があります。ただし、src が別のドメインからのものである場合は、例外がスローされます。これは、クロスサイト スクリプティングの脆弱性を引き起こす、他人のページで任意の JavaScript を実行できないようにするセキュリティ機能です。これは、私が話していることを示すコードの例です。
<script src="http://prototypejs.org/assets/2009/8/31/prototype.js" type="text/javascript"></script>
<h1>Parent</h1>
<script type="text/javascript">
function on_load(iframe) {
try {
// Displays the first 50 chars in the innerHTML of the
// body of the page that the iframe is showing.
// EDIT 2012-04-17: for wider support, fallback to contentWindow.document
var doc = iframe.contentDocument || iframe.contentWindow.document;
alert(doc.body.innerHTML.substring(0, 50));
} catch (e) {
// This can happen if the src of the iframe is
// on another domain
alert('exception: ' + e);
}
}
</script>
<iframe id="child" src="iframe_content.html" onload="on_load(this)"></iframe>
例をさらに進めるために、これを iframe のコンテンツとして使用してみてください。
<h1>Child</h1>
<a href="http://www.google.com/">Google</a>
<p>Use the preceeding link to change the src of the iframe
to see what happens when the src domain is different from
that of the parent page</p>