Works in Firefox but not in IE6, any help appreciated....

D

DamonChong

Hi,

Thanks alot in advance. I got the following two functions in my
javascript which work fine in Firefox 1.5 but not in IE6. Can someone
please help? Thank you again.

--------------------snip---------------------
function myClass(){
var self = this;
}

function defaultAction(){
myClass.prototype.callOut = eval("function (){ " +
"alert('testing myClass'); " +
"}");
myObject = new myClass();
if( typeof(myObject) == 'undefined' ){
alert("myObject is not defined");
}
else if( typeof(myObject) == 'function' ){
alert("myObject is a function");
}
else{
alert("myObject is " + typeof(myObject));
}
for (property in myObject) {
alert("myObject has property - " + property);
}
myObject.callOut(); // Problem appear here. IE complains Object
Expected
}
 
R

RobG

DamonChong said:
Hi,

Thanks alot in advance. I got the following two functions in my
javascript which work fine in Firefox 1.5 but not in IE6. Can someone
please help? Thank you again.

A couple of tips when posting code: do not use tabs, use 2 or 4 spaces
for indentation and manually wrap code at about 70 characters to
prevent auto-wrapping. Also, reduce your test case to the minimum
required (though here that isn't much of an issue).

--------------------snip---------------------
function myClass(){
var self = this;
}

function defaultAction(){
myClass.prototype.callOut = eval("function (){ " +
"alert('testing myClass'); " +

Eval is almost never required:

myClass.prototype.callOut = function (){
alert('testing myClass');
}

"}");
myObject = new myClass();
if( typeof(myObject) == 'undefined' ){
alert("myObject is not defined");
}
else if( typeof(myObject) == 'function' ){
alert("myObject is a function");
}
else{
alert("myObject is " + typeof(myObject));
}
for (property in myObject) {
alert("myObject has property - " + property);
}
myObject.callOut(); // Problem appear here. IE complains Object
Expected
}

The following works fine in Firefox and IE 5.2:

function myClass(){
var self = this;
}


function defaultAction(){
myClass.prototype.callOut = function (){
alert('testing myClass');
}

myObject = new myClass();

alert("myObject is " + (typeof myObject) );

for (property in myObject) {
alert("myObject has property - " + property);
}

myObject.callOut();

}

defaultAction();
 
D

Damon Chong

Thanks alot Rob. The problem is I need the eval() method as I don't
know what is the functions that might be created dynamically during
runtime. In this case, I simply coded the eval("function().....") but
in future it could be like below,

myClass.prototype.callOut = eval(strval); // where strval is a String
variable

I guess it is abit convulated but surprising it works fine in Firefox.
Is this way of dynamically creating a function supported in IE 5 and
above?

Thank you again.
 
R

RobG

Damon said:
Thanks alot Rob. The problem is I need the eval() method as I don't
know what is the functions that might be created dynamically during
runtime. In this case, I simply coded the eval("function().....") but
in future it could be like below,

myClass.prototype.callOut = eval(strval); // where strval is a String
variable

I guess it is abit convulated but surprising it works fine in Firefox.
Is this way of dynamically creating a function supported in IE 5 and
above?

I can't comment on that in general, I'm testing on Mac OS at the
moment so IE 5.2 is all I can do.

What you are trying to do does not appear to be much different from
using JSON, here is another alternative (tested in Safari, Firefox &
IE 5.2):

<script type="text/javascript">

var myObject;
var funcBody = "alert('testing myClass')"

function myClass(){
var self = this;
}

function defaultAction()
{
myClass.prototype.callOut = new Function(funcBody);

myObject = new myClass();

for (property in myObject) {
alert("myObject has property - " + property);
}
myObject.callOut();
}

defaultAction();

</script>


There is a (rather long) discussion on eval and JSON here, but Jim
Ley's resonponse seems pretty good:

<URL:
http://groups.google.com/group/comp...5d49557a7?q=JSON+eval&rnum=3#f870fb75d49557a7
 
D

Damon Chong

Thanks alot Rob, I tried it out on IE6 and it is working now. Your tips
were very helpful in solving my problem. Thanks again! =D
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top