サブドメインからのソースを含むiframeタグがロード時にドキュメントに既に存在する場合、IE9からアクセスできますが、同じiframeがドキュメントに挿入されている場合、IE9はアクセスできません(他のすべてのブラウザーはアクセスできます)。以下のコードスニペットをご覧ください。
それは通常のIE9の動作ですか、それとも何らかの方法で修正できますか?今回はグーグルが役に立たなかったので、ここで助けていただければ幸いです。
これは、InternetExplorer9および他のすべてのブラウザーで機能します
example.comのメインドキュメント:
<div class="container">
<iframe src="sub.example.com/index.html"></iframe>
</div>
<script>
document.domain = 'example.com';
var frame = $('iframe', 'container')
, el = frame.contents().find('div.hello'); // usually returns 1 element
if (el.length > 0)
el.html('Hello'); // sets div content in iframe
else // sometimes it gets here, if script runs before iframe loads
frame.on('load', function(){
el = frame.contents().find('div.hello'); // works if the 1st one fails
el.html('Hello');
});
</script>
サブドメイン(sub.example.com/index.html)のドキュメント:
<script>document.domain = 'example.com'</script>
<div class="hello"></div>
これはすべてのブラウザで機能しますが、IE9では機能しません(エラー:アクセスが拒否されました)
example.comのメインドキュメント:
<div class="container"></div>
<script>
$('div.container').html('<iframe src="sub.example.com/index.html"></iframe>');
document.domain = 'example.com';
var frame = $('iframe', 'container')
, el = frame.contents().find('div.hello'); // throws error
// ...
</script>
サブドメインのドキュメントは同じです。
実際のコードでは、.containerの内容はテンプレート関数によって生成されるため、例のように.html()を使用して単純なテキストとして挿入されます。
IEで2番目のケースを機能させるために助けてくれてありがとう。