此代码在IE6中第10行给出错误。也就是var ref = ...;
这里有什么错误?
<html>
<head>
<title>JavaScript Popup Example 3</title>
</head>
<SCRIPT language="JavaScript1.2">
function MyClass()
{
this.OpenWindow = function()
{
var ref = window.open ("http://www.google.com", "mywindow", "location=1,status=1,scrollbars=1,width=100,height=100");
ref.moveTo(0,0);
}
}
</SCRIPT>
<body onload="javascript: new MyClass().OpenWindow()">
<H1>JavaScript Popup Example 3</H1>
</body>
</html>
消息:
A run-time error has occurred.
Do you wish to debug?
Line:10
Error: Access is denied
答案 0 :(得分:6)
当您打开包含来自其他域的页面的窗口时,您不会获得对该窗口的引用。 ref变量为null。
如果你想移动窗口,你必须在没有页面的情况下打开它,移动它,然后在其中加载页面:
var r = window.open ('', 'mywindow', 'location=1,status=1,scrollbars=1,width=100,height=100');
r.moveTo(0,0);
r.location.href = 'http://www.google.com';
答案 1 :(得分:0)
问题出在这里 - ref.moveTo(0,0); - 在大多数安全设置上,此操作不可用
另外,javascript:on onload只会创建一个标签“javascript”
<html>
<head>
<title>JavaScript Popup Example 3</title>
</head>
<SCRIPT language="JavaScript">
function MyClass()
{
this.OpenWindow = function()
{
var ref = window.open("http://www.google.com", "mywindow", "location=1,status=1,scrollbars=1,width=100,height=100");
ref.moveTo(0,0);
}
}
</SCRIPT>
<body onload="new MyClass().OpenWindow()">
<H1>JavaScript Popup Example 3</H1>
</body>
</html>