Window.frameElement is null.

write an iframe src=child.html in the page parent.html and return null using window.frameElement in child.html

the environmental background of the problems and what methods you have tried

related codes

parent.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h3></h3>
    <iframe id="iframe" src="./child.html" width="500" height="300"></iframe>
    <script src="./parent.js"></script>
</body>
</html>

child.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h3></h3>
    <script src="./child.js"></script>
</body>
</html>

child.js

console.log("iframe window.frameElement", window.frameElement);

iframe is a window but not a top-level window, so it should not be null.

The

MDN says, "return the elements embedded in the current window object (such as < iframe > or < object >), and return null. if the current window object is already a top-level window."
the problem is that child.js is not a top-level window, why is it also null?? Or is there something wrong with my understanding?

Feb.09,2022

Brother, have you found the cause of this problem? Is it convenient to explain if you find it


The
window.frameElement attribute is mainly used when the current window is embedded in another web page (embedding < object >, < iframe > or < embed > elements) to return the element node where the current window is located. This property returns null if the current window is a top-level window, or if the embedded web page is not of the same origin.
For more information, please see 2.32.5
http://javascript.ruanyifeng.

.
Menu