鍍金池/ 問答/HTML/ alert(opener.name)為何沒有結(jié)果?

alert(opener.name)為何沒有結(jié)果?

教材上說:alert(opener.name)可以獲得父窗口的名字。

cat father.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <script>
    window.open('file:///tmp/child.html');
    </script>
</body>
<p>i am father window</p>
</html>

cat child.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script>
        alert(opener.name);
    </script>
</head>
<body>
    <p>i am child window</p>
</body>
</html>

瀏覽器中輸入 file:///tmp/father.html ,child.html也會打開,但是為何沒有alert窗口打開?

回答
編輯回答
孤星

opener未定義,獲取opener.name時候直接報錯:Uncaught TypeError: Cannot read property 'name' of null,所以不會alert

2018年1月10日 23:48
編輯回答
巷尾

丟服務(wù)器去執(zhí)行吧

2018年2月11日 19:44
編輯回答
尐懶貓

可能是瀏覽器安全策略把? 你這個肯定報錯啊,看看報錯信息就知道了。

文件協(xié)議這樣操作獲取是不行的,最起碼換成http協(xié)議

2018年5月31日 20:32