简体   繁体   中英

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]

If I edit the file with "Frame 2" this way:

<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

Until now, everything works as expected. But if I edit the file with "Frame 2" this other way:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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