window.open并向其添加属性

时间:2016-10-22 11:29:29

标签: javascript jquery html dom

我在index.html中有代码:

<!DOCTYPE html>
<html>
<head>
<script>
function createDialogBox()
{
var dialogBox = window.open("in1.html");
dialogBox.nameBox= "my little box"
}

window.onload = createDialogBox;
window.onclick = createDialogBox;//when event is click evrything is okand i can get nameBox from in1.html via window.nameBox
</script>
</head>
<body>
Parent Page
</body>
</html>

对话框:

<!DOCTYPE html>
<html>
<head>
<script>
console.log(window.nameBox)// undefined
</script>
<body>
DialogBox Page
</body>
</html>

为什么我在尝试加载in1.html并使用window.onload时无法获取nameBox?这两个文件都在我的电脑中并直接运行文件。两个文件都在同一个文件夹中

1 个答案:

答案 0 :(得分:-1)

实际上你正在创建window.open("in1.html");的对象 如果你想命名父页面名称框,试试这个

 function createDialogBox() {
            var dialogBox = window.open("HtmlPage2.html");
            dialogBox.nameBox = "my little box"
            this.nameBox="parent little box"
        }