0

ページからfile://localhost/Users/pistacchio/dev/epress/catflow/test_html/index.html、iframe にアクセスしようとしている次の (coffeescript) コードがあります。

$('#ipad-viewport iframe').bind 'load', () ->
    console.log $(this).contents().find('map')

(これは次の JavaScript に変換されますが、問題はここに依存しているとは思いません):

(function() {

  $('#ipad-viewport iframe').bind('load', function() {
    return console.log($(this).contents().find('map'));
  });

}).call(this);

iframe ページが読み込まれるのを待って、その本文内の要素にアクセスしようとします。次のエラーが表示されます。

Unsafe JavaScript attempt to access frame with URL file://localhost/Users/pistacchio/dev/epress/catflow/test_html/catalogo/catalog/intro.html from frame with URL file://localhost/Users/pistacchio/dev/epress/catflow/test_html/index.html. Domains, protocols and ports must match.

さて、iframeは次のように定義されているので:

<iframe src="file://localhost/Users/pistacchio/dev/epress/catflow/test_html/catalogo/catalog/intro.html" width="1024" height="768"></iframe>

ページと iframe の両方が同じドメインにありfile://localhostませんか? この問題が発生するのはなぜですか?

ああ、関連する場合は、Chrome 18 でこれをテストしています。

4

1 に答える 1

5

file:///same origin policyURL には、ホストされているコンテンツに適用される通常とは少し異なる JavaScript セキュリティ ポリシーが適用されます。保存された Web ページがディスクの内容全体を読み取れないようにするために、異なるファイルは異なるオリジンとして認識されます。ローカル サーバーを起動して、その上でコンテンツをホストするだけです。オリジンがドメイン/IP によって定義される「標準」ポリシーにフォールバックします。

何らかの理由で Web サーバーを実行できない場合は、コマンド ライン スイッチ--allow-file-access-from-files. これには、すべてのfile:///URL を同じオリジンに属するものとして定義する効果があると思います。

于 2012-05-09T12:47:08.787 に答える