Ann Sanderson
Ann Sanderson

Reputation: 407

Getting HTML Parent Window Won't Work. Suggestions?

I have the following two HTML Documents:

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

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

Main.html brings up Exam.html via the input button. From inside Exam.html I would like to change the variable ExamID on the parent document (i.e.: Main.html). I'm trying to do this via the JavaScript function: setParentInfo().

The above code is not working. Can someone help me come up with the correct code?

Thanks So Much!

Upvotes: 0

Views: 171

Answers (2)

Mohammod Hossain
Mohammod Hossain

Reputation: 4114

Variable is declared and assigned in parent window so you get reference from your child window.

you can test using alert statement:

alert(window.parent.document.ExamId);

//output::001B

Upvotes: 0

Brian Nickel
Brian Nickel

Reputation: 27550

Variables are assigned on the window object, not the document object.

Since the value is already set, you can instead read the existing value to verify it:

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

Upvotes: 2

Related Questions