简体   繁体   English

帧之间的通信

[英]Communication between frames

I'm trying to simplify my problem, and I don't think it could be simpler than this:我试图简化我的问题,我认为它不会比这更简单:

<html>
<frameset rows = "50%,50%" name="proof">
<frame src="one.html" name="one">
<frame src="two.html" name="two">
</frameset>
</html>

<html>
<body>
<H1>Frame 1</H1>
</body>
</html>

<html>
<body>
<H1>Frame 2</H1>
</body>
</html>

This is what I see when I load the frameset:这是我在加载框架集时看到的:

[https://ibb.co/LSMpgZF] [https://ibb.co/LSMpgZF]

If I edit the file with "Frame 2" this way:如果我以这种方式使用“Frame 2”编辑文件:

<html>
<body>
<H1>Frame 2</H1>
<script>
alert (top.two.name);
</script>
</body>
</html>

and I reload the frameset, I see this:我重新加载框架集,我看到了这个:

https://ibb.co/Qkkhp69 https://ibb.co/Qkkhp69

Until now, everything works as expected.到目前为止,一切都按预期工作。 But if I edit the file with "Frame 2" this other way:但是,如果我以其他方式使用“Frame 2”编辑文件:

<html>
<body>
<H1>Frame 2</H1>
<script>
alert (top.one.name);
</script>
</body>
</html>

no alert appears, despite having done exactly what everyone on the Internet is suggesting.尽管已经完全按照 Internet 上的每个人的建议进行操作,但没有出现警报。 The same happens with the other frame: it can display its own name, but not the other's (or the frameset's).另一个框架也是如此:它可以显示自己的名称,但不能显示其他(或框架集)的名称。

Why is that?这是为什么? How can the two frames communicate?两个框架如何通信?

OK, I solved the issue myself.好的,我自己解决了这个问题。

I tried many variations, and above all I placed both alerts in the same html file.我尝试了许多变体,最重要的是我将两个警报放在同一个 html 文件中。 I noticed the alert displaying its own name was correctly executed only if it was the first one.我注意到只有当它是第一个时才正确执行显示其自己名称的警报。 So I guessed there was a JS error somewhere in the other alert and indeed there was one - I eventually located the error in the Firefox console.所以我猜想在另一个警报的某个地方有一个 JS 错误,确实有一个 - 我最终在 Firefox 控制台中找到了错误。 It was caused by a browser security setting, as I suspected, and some good solutions can be found in this other thread: Disable firefox same origin policy正如我所怀疑的那样,这是由浏览器安全设置引起的,并且可以在其他线程中找到一些好的解决方案: 禁用 firefox 同源策略

This worked perfectly for me (as I'm using Firefox): after Firefox 68 I need to adjust 'privacy.file_unique_origin' -> false (by open 'about:config') to solve 'CORS request not HTTP' for new CORS same-origin rule introduced.这对我来说非常有效(因为我使用的是 Firefox):在 Firefox 68 之后,我需要调整 'privacy.file_unique_origin' -> false(通过打开 'about:config')来解决新 CORS 的“CORS 请求不是 HTTP” -引入了原点规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM