簡體   English   中英

獲取HTML父窗口將無法正常工作。 建議?

[英]Getting HTML Parent Window Won't Work. Suggestions?

我有以下兩個HTML文檔:

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通過輸入按鈕調出Exam.html。 從Exam.html內部我想更改父文檔中的變量ExamID(即:Main.html)。 我正在嘗試通過JavaScript函數執行此操作:setParentInfo()。

上面的代碼不起作用。 有人可以幫我提出正確的代碼嗎?

非常感謝!

變量在window對象上分配,而不是document對象。

由於該值已設置,您可以改為讀取現有值以進行驗證:

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

在父窗口中聲明和分配變量,以便從子窗口獲取引用。

你可以使用alert語句測試:

alert(window.parent.document.ExamId);

//output::001B

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM