How to call a JavaScript function from one frame to another in Chrome/Webkit with file protocol

时光总嘲笑我的痴心妄想 提交于 2019-12-24 06:13:19

问题


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).

回答1:


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.




回答2:


I tried your test pages, and they work fine with Chrome 4.1.249.1045 on Windows (and Firefox 3.6.3, and IE7 [after fixing the issue below]). So I'm with Pekka (as usual): I think the problem must lie elsewhere.

It wasn't working in IE7 and it took me forever to figure out why not: You need to give either rows or cols on the frameset tag, otherwise IE only loads the one frame. (The validator would have told me that if I'd asked it.)



来源:https://stackoverflow.com/questions/2570718/how-to-call-a-javascript-function-from-one-frame-to-another-in-chrome-webkit-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!