如何在javascript中找到父窗口的标题?

时间:2013-01-19 10:06:28

标签: c# javascript asp.net

如何在javascript中找到父窗口的标题?

我用

 alert(window.parent.document.FormName);  

 alert(window.parent.document.title);

 alert(window.parent.parent.document.title);

但结果并非如此。

2 个答案:

答案 0 :(得分:1)

您可能正在尝试从另一台服务器上的parent访问iframe窗口的标题。出于安全原因,这是不可能的。

答案 1 :(得分:1)

将此代码用于您的解决方案

  1. pageMain.html

    <html>
    <head>
        <title>Parent Window</title>
    </head>
    <body>
    <input type="text" id="data" value="23444" />
    <a href="#" onclick="javascript:openChildWindow();">Open Child Popup window</a>
    </body>
    <script>
    function openChildWindow() {
        window.open('page1.html','childWindow','width=400,height=400');
    }
    </script>
    </html>
    
  2. page1.html

    <html>
    <head>
        <title>Child Window</title>
    </head>
    <body onload="initializeMainDiv();">
        <div id="mainDiv"></div>
    </body>
    <script>
    function initializeMainDiv() {
       alert( window.opener.document.title )
      }
      </script>
    </html>