如何使用文件协议在Chrome / Webkit中从一帧到另一帧调用JavaScript函数 [英] How to call a JavaScript function from one frame to another in Chrome/Webkit with file protocol

查看:172
本文介绍了如何使用文件协议在Chrome / Webkit中从一帧到另一帧调用JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个应用程序,它有一个框架中的项目列表;当点击一个项目时,它会在另一个框架中执行某些操作(加载图像)。



这适用于所有浏览器,包括Chrome 3;现在它在Firefox中仍然可以正常工作,但是在最近的Chrome版本中(我相信自4以后)它会抛出这个错误:


不安全的JavaScript尝试访问来自具有URL(...)的帧的URL(...)。
域名,协议和端口必须匹配。

这显然是一个安全功能,但可以绕过它是?

以下是一个简单的测试:

index.html:

 < html> 
< frameset cols =50%,50%>
< frame src =left.htmlname =left/>
< frame src =right.htmlname =right/>
< / frameset>
< / html>

left.html:

 < HTML> 
< body>
< a href =javascript:parent.right.test('hello');>点击我< / a>
< / body>
< / html>

right.html:

 < HTML> 
< body>
< script>
function test(msg){
alert(msg);
}
< / script>
< / body>
< / html>

以上作品在Firefox 3.6和Chrome 3中,但在Chrome 5中引发了上述错误...
$ b 编辑


  • 属性到框架元素

  • 实际上,它在Chrome中工作当且仅当页面使用http协议(并且来自同一个域)时,我的问题是当页面是本地的,由文件://协议提供。然后,它适用于Firefox(所有版本)和Chrome 3,但不适用于Chrome 5(我没有Chrome 4,所以我不确定这个特定版本(并且不知道是否可以下载特定的Chrome版本?) - 但对于Chrome 5,我确信它不起作用)。
  • 查看与密切相关的问题的答案:使用文件协议调用Chrome中的iframe中定义的JavaScript函数简单地说,使用 - 允许从文件访问文件解决问题,因为错误不会被报告。

    当然,由于您在CD上分发文件,因此您不太可能会将其视为实际的解决方案。我建议您主演 Chromium bug 47416 ,鼓励Chromium的开发者让Chrome更符合Gecko的行为。


    I have developed an application that has a list of items in one frame; when one clicks on an item it does something in another frame (loads an image).

    This used to work fine in all browsers, including Chrome 3; now it still works fine in Firefox but in recent versions of Chrome (I believe since 4) it throws this error:

    Unsafe JavaScript attempt to access frame with URL (...) from frame with URL (...). Domains, protocols and ports must match.

    This is obviously a security "feature" but is it possible to get around it?

    Here is a simple test:

    index.html:

    <html>
      <frameset cols="50%,50%">
        <frame src="left.html" name="left"/>
        <frame src="right.html" name="right"/>
        </frameset>
      </html>
    

    left.html:

    <html>
      <body>
        <a href="javascript:parent.right.test('hello');">click me</a>
        </body>
      </html>
    

    right.html:

    <html>
      <body>
        <script>
          function test(msg) {
            alert(msg);
            }
          </script>
        </body>
      </html>
    

    The above works in Firefox 3.6 and Chrome 3 but in Chrome 5 it throws the above error...

    Edit:

    • added the @cols attribute to the frameset element
    • in fact it works in Chrome if and only if the pages are served with the http protocol (and from the same domain) but my problem is when pages are local and served from a file:// protocol. Then it works in Firefox (all versions) and Chrome 3 but not Chrome 5 (I don't have Chrome 4 so I'm not shure about that specific version (and don't know if it's even possible to download a specific Chrome version?) -- but for Chrome 5 I'm very sure it doesn't work).

    解决方案

    See the answers to the closely-related question: Call a JavaScript function defined in an iframe in Chrome using the file protocol.

    Briefly, launching Chrome with --allow-file-access-from-files "solves" the problem insofar as the error will not be reported.

    Of course, since you're distributing files on a CD, you're unlikely to view this an actual solution. I recommend starring Chromium bug 47416 to encourage the developers of Chromium to bring Chrome more into line with the behavior of Gecko.

    这篇关于如何使用文件协议在Chrome / Webkit中从一帧到另一帧调用JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆