获取 HTML 父窗口将不起作用.建议

Getting HTML Parent Window Won't Work. Suggestions?

本文关键字:不起作用 建议 窗口 HTML 获取      更新时间:2023-09-26

我有以下两个HTML文档:

主.html

<html lang="eng">
<head>
  <title>JavaScript Example</title>
  <script type="text/javascript">
    var ExamId = "001A";
    function open_exam()
    {
      window.open("exam.html")
    }
  </script>
</head>
  <body>
    <input type=button value="Open Exam" onclick="open_exam()">
  </body>
</html>

考试.html

<html lang="eng">
<head>
  <title>JavaScript Example</title>
  <script type="text/javascript">
    function setParentInfo()
    {
        window.parent.document.ExamID = '001B';
    }
  </script>
</head>
  <body>
    <p>Welcome to the Exam!</p>
    <input type=button value="Set Parent Info" onclick="setParentInfo()">
  </body>
</html>

主.html通过输入按钮调出考试.html。 在考试内部.html我想更改父文档上的变量 ExamID(即:Main.html)。 我正在尝试通过JavaScript函数来执行此操作:setParentInfo()。

上面的代码不起作用。 有人可以帮我想出正确的代码吗?

非常感谢!

变量是在window对象上分配的,而不是在document对象上分配的。

由于该值已设置,因此您可以改为读取现有值进行验证:

alert(window.parent.ExamId); // == "001A"
变量

在父窗口中声明和赋值,因此您可以从子窗口获取引用。

您可以使用alert语句进行测试:

alert(window.parent.document.ExamId);
//output::001B