2

This code is giving me error on line 10 in IE6. That is, var ref = ...;

What is the error here?

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

The message:

A run-time error has occurred. 
Do you wish to debug? 

Line:10
Error: Access is denied
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
user366312
  • 16,949
  • 65
  • 235
  • 452
  • javascript line numbers in IE can be misleading. what is the actual error, and what are the visible symptoms? what happens when the code is executed? the first thing i notice is that `onload` doesn't need the `javascript:` prefix, as it is a javascript event handler, but it shouldn't cause an error, either – David Hedlund Nov 29 '09 at 14:37
  • No. Removing javascript: is not working. The error is being shown up again. – user366312 Nov 29 '09 at 14:41
  • It would be really stupid if this is the cause, but what are the chances its that space between "window.open" and the bracket? – Rory Nov 29 '09 at 14:42
  • @JMSA: indeed, it shouldn't resolve the issue. i'd still like to know what the actual error message that is showing up is. – David Hedlund Nov 29 '09 at 14:46
  • Message:"A run-time error has occurred. Do you wish to debug? Line:10 Error: Access is denied" – user366312 Nov 29 '09 at 14:51

2 Answers2

6

When you open a window with a page from a different domain, you don't get a reference to the window back. The ref variable is null.

If you want to move the window, you have to open it without a page, move it, then load the page in it:

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';
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

the problem is here - ref.moveTo(0,0); - on most security settings this action is unavailable

also, javascript: on onload just creates a label "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>
glebm
  • 20,282
  • 8
  • 51
  • 67