Connecting Tech Pros Worldwide Forums | Help | Site Map

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

DamonChong
Guest
 
Posts: n/a
#1: Dec 28 '05
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
}


RobG
Guest
 
Posts: n/a
#2: Dec 28 '05

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


DamonChong wrote:[color=blue]
> 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.
>[/color]

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

[color=blue]
> --------------------snip---------------------
> function myClass(){
> var self = this;
> }
>
> function defaultAction(){
> myClass.prototype.callOut = eval("function (){ " +
> "alert('testing myClass'); " +
>[/color]

Eval is almost never required:

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

"}");[color=blue]
> 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
> }
>[/color]

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();



--
Rob
Damon Chong
Guest
 
Posts: n/a
#3: Dec 28 '05

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


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.

RobG
Guest
 
Posts: n/a
#4: Dec 28 '05

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


Damon Chong wrote:[color=blue]
> 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?[/color]

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....70fb75d49557a7[color=blue]
>[/color]

--
Rob
Damon Chong
Guest
 
Posts: n/a
#5: Dec 29 '05

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


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

Closed Thread