Javascript 错误:window.opener';s属性未定义

Javascript 错误:window.opener';s属性未定义,javascript,html,Javascript,Html,一个页面使用window.open()方法打开另一个页面(例如openerdemo.html),但弹出页面无法访问opener页面的任何属性 起始页代码: <head> <meta http-equiv="content-type" content="html/text"; charset="utf-8" > <title>windowdemo</title> <script language="JavaScrip

一个页面使用window.open()方法打开另一个页面(例如openerdemo.html),但弹出页面无法访问opener页面的任何属性

起始页代码:

<head>
    <meta http-equiv="content-type" content="html/text"; charset="utf-8"  >
    <title>windowdemo</title>

    <script language="JavaScript">

        function openWin(thisurl) {
            popWin = window.open(thisurl, 'popupPage', "width=480,height=272");  
        }

    </script>
</head>

<body>
    <input type="button" value="open" onClick="openWin('openerdemo.htm')"/>
</body>

窗口演示
函数openWin(thisurl){
popWin=window.open(这个URL,'popupPage','width=480,height=272');
}
弹出页面(openerdemo.htm)代码:


窗口演示
函数closeWin(){
window.opener.close();
window.close();
}

我在Chrome中使用javascript控制台,在弹出窗口的cmd行中输入'window.opener',返回:

开窗器
'窗口{}',

这意味着opener窗口不为null,但缺少其all属性。但是,如果一个页面打开一个新页面,如下所示:

popWin=window.open(“”,'popupPage','width=480,height=272”)
popWin.document.write(“这是popupPage”)

弹出页面的window.opener是对opener窗口的引用,并且可以使用“window.opener”对象控制opener窗口。 例:


myWindow=window.open('','',宽度=200,高度=100')
myWindow.document.write(“这是‘myWindow’”)
myWindow.focus()
myWindow.opener.document.write(“这是父窗口”)

我在FF、IE和chrome中测试这段代码


有人能告诉我如何控制弹出页面中的“开启器”窗口吗?

它可以正常工作,但您正在发出跨域请求。如果您正在打开的窗口和从中打开它的窗口位于同一个域上,则不会出现问题

注意:如果您不使用Web服务器,而只是使用文件系统(file:///是您的协议),则这可能被归类为跨域请求。我还没有测试过它——不过,请放心,当您在web上获得它时,只要opener和openee都是来自同一域的服务器,一切都会很好

编辑

我刚刚在我的本地文件系统上做了一个快速测试,事实确实如此——它被归类为跨域请求,出于安全目的是被禁止的——同样,当您将它放在Web服务器上并为来自同一域的两个页面提供服务时,这不会是一个问题

<html>
<head>
    <meta http-equiv="content-type" content="html/text"; charset="utf-8"  >
    <title>windowdemo</title>

    <script language="JavaScript">

        function closeWin() {
            window.opener.close();
            window.close();
        }
    </script>
</head>

<body>
<h1><a href="#" onClick="closeWin()">close all</a></h1>
</body>
<body>

<script type="text/javascript">
myWindow=window.open('','','width=200,height=100')
myWindow.document.write("This is 'myWindow'")
myWindow.focus()
myWindow.opener.document.write("This is the parent window")
</script>

</body>