不安全的JavaScript尝试使用URL访问框架(同一个域!)
从页面file://localhost/Users/pistacchio/dev/epress/catflow/test_html/index.html
我有以下(coffeescript)代码试图访问iframe:
$('#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在同一个域中,还是file://localhost
? 为什么我遇到这个问题?
哦,如果相关,我正在使用Chrome 18进行测试。
file:///
URL same origin policy
适用于托管内容的普通same origin policy
略有不同的javascript安全策略。 为了阻止已保存的网页能够读取磁盘的全部内容,不同的文件被视为不同的来源。 只需启动本地服务器并在其上托管您的内容; 您将回归到“标准”策略,其中起源由domain / ip定义。
如果由于某种原因您无法运行Web服务器,您可能会从命令行开关中获得一些--allow-file-access-from-files
: – --allow-file-access-from-files
。 我相信这会使所有file:///
URL被定义为属于同一个来源。