martin_narg
09-20-2005, 12:15 PM
Hey guys, I came across what looks like unusual results in Gecko and was wondering if someone could explain it.
Essentially, if an attribute is set in a js object to be a native window function (such as alert or prompt), then executed by calling that object attribute, it errors out in Gecko but runs normally in IE. I'm curious as to why that would be. My thoughts are that perhaps it is a reserved top-level keyword and thus the reference cannot be saved as a local attribute of an object, or perhaps that native (or inbuilt) window functions and attributes cannot be overwritten or cloned (or the reference cloned) in any way as a security measure - but this is pure guesswork.
I would have thought that the object-oriented nature of the language would mean that it is possible to manipulate functions/objects regardless of their native position within the framework of the DOM - except the framework objects such as window, document, element etc.
Here's a simplified test page:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
function runFunction(fn) {
fn("foo");
}
function myObject(fn) {
this.fn = fn;
this.fn("foobar");
}
function myAlert() {
alert("bar");
}
function init() {
runFunction(alert); // alerts foo in both IE and Gecko
var obj1 = new myObject(myAlert); // alerts bar in both IE and Gecko
var obj2 = new myObject(alert); // alerts foobar in IE, errors out in Gecko "Illegal operation on WrappedNative prototype object"
}
</script>
</head>
<body>
<form name="frm">
<input type="button" name="btn" value="click" onclick="init();">
</form>
</body>
</html>
Many thanks for any feedback or ideas.
m_n
Essentially, if an attribute is set in a js object to be a native window function (such as alert or prompt), then executed by calling that object attribute, it errors out in Gecko but runs normally in IE. I'm curious as to why that would be. My thoughts are that perhaps it is a reserved top-level keyword and thus the reference cannot be saved as a local attribute of an object, or perhaps that native (or inbuilt) window functions and attributes cannot be overwritten or cloned (or the reference cloned) in any way as a security measure - but this is pure guesswork.
I would have thought that the object-oriented nature of the language would mean that it is possible to manipulate functions/objects regardless of their native position within the framework of the DOM - except the framework objects such as window, document, element etc.
Here's a simplified test page:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
function runFunction(fn) {
fn("foo");
}
function myObject(fn) {
this.fn = fn;
this.fn("foobar");
}
function myAlert() {
alert("bar");
}
function init() {
runFunction(alert); // alerts foo in both IE and Gecko
var obj1 = new myObject(myAlert); // alerts bar in both IE and Gecko
var obj2 = new myObject(alert); // alerts foobar in IE, errors out in Gecko "Illegal operation on WrappedNative prototype object"
}
</script>
</head>
<body>
<form name="frm">
<input type="button" name="btn" value="click" onclick="init();">
</form>
</body>
</html>
Many thanks for any feedback or ideas.
m_n