No, there is no other way. top
automatically refers to the topmost window in the window hierarchy. Since it's read-only (to prevent an iframe from accessing the parent document's window
), there's no way to use the window.top
property. You will either have to replace all top
usages with self
(which is not read-only or reserved), or place your entire script in a different scope.
Since the window.top
property is a property of the window
object, it is automatically a global variable. If you define a variable outside any functions (in the global scope), it'll be a property of the window
object, so it's not possible to define var top
in the global scope, since that'd have to overwrite the read-only window.top
. If you were to use var top
inside a function though, it'd work just fine, as can be demonstrated in this example:
function foo() {
var top = 'test';
return top;
}
alert(foo()); //Alerts "test"
In the future, it's best to avoid overly generic variable names such as top
. They can easily cause problems. Especially when they need to be global variables.