instantiate class problem on IE

35 views
Skip to first unread message

Loris

unread,
Dec 18, 2009, 6:36:14 PM12/18/09
to Prototype & script.aculo.us
hello, sorry for my bad English.
I try to explain:

I try to instantiate a class when the page loads, but when I happened
on most browsers, I can not do it in Internet Explorer 7 or 6

I call the script in my template, like this:

<script type="text/javascript" src="/js/veicoli-random.class.js"></
script>

<script type="text/javascript">

new VeicoliRandom('wrap-cont_veicoli_random',
'cont_veicoli_random', '{$url_pagina}');

</script>


And the script contains:

Random = Class.create();

Random.prototype = {

wrap : null,
nomeWrap : null,
container : null,
nomeContainer : null,
url : null,

initialize : function(wrap, container, url)

{
this.wrap = $(wrap);
this.nomeWrap = wrap;

this.container = $(container);
this.nomeContainer = container;
this.url = url;

if ( this.url == '' )
this.url = '/';

if (!this.container || !this.wrap)

return;


this.arrotola.bind(this).delay(10);

},

arrotola : function()
{
// nascondo la scritta sold, per IE
var ElementiNIE = $$('#' + this.nomeContainer + ' div.timbro-venduto-
random');
ElementiNIE.each( function(div) {
div.addClassName('invisibileIE');
});

// arrotolo su
Effect.BlindUp(this.wrap, { duration: 3 });
this.aggiorna.bind(this).delay(5);
},

aggiorna : function ()
{
// aggiorno
var options = {onComplete : this.srotola.bind(this).delay(1)};
new Ajax.Updater(this.container, this.url, options);
},

srotola : function()
{
this.wrap.setStyle({ display: 'none' });
// srotolo giù
Effect.BlindDown(this.wrap, { duration: 3 });
this.arrotola.bind(this).delay(15);
},

};


Object.extend(document, {
isDocReady: false,
isDocLoaded: false,
ready: function(fn) { Event.observe(document, "doc:ready", fn); },
load: function(fn) { Event.observe(document, "doc:loaded", fn); }
});
Event.observe(document, "dom:loaded", function() {
Event.fire(document, "doc:ready");
document.isDocReady = true;
if (document.isDocLoaded)
Event.fire(document, "doc:loaded");
});
Event.observe(window, "load", function() {
document.isDocLoaded = true;
if (!document.isDocReady) return;
Event.fire(document, "doc:loaded");
});


// fa partire la vera classe al load della pagina
VeicoliRandom = Class.create();

VeicoliRandom.prototype = {

initialize : function(wrap, container, url)

{

// Event.observe(window, 'load', new Random(wrap, container, url));
//document.observe('dom:loaded', new Random(wrap, container, url));
document.load( a = new Random(wrap, container, url) );

},

};


I can not understand why the class called "random" is not instantiated
or otherwise IE does nothing.
While with fire-fox or other browsers, seems to work properly.
What could be my mistake?

Thank you for your attention.
Regards Loris

david

unread,
Dec 22, 2009, 1:17:38 PM12/22/09
to Prototype & script.aculo.us
Hi Loris,

I think that your trouble is normal, because IE don't allow to
instantiate method on native object.
Generally it's not a good idea to extend native objects because your
not sure that another JS will not use the same method name !!

--
david

RobG

unread,
Dec 22, 2009, 9:31:51 PM12/22/09
to Prototype & script.aculo.us

On Dec 23, 4:17 am, david <david.brill...@gmail.com> wrote:
> Hi Loris,
>
> I think that your trouble is normal, because IE don't allow to
> instantiate method on native object.

You might need to re-think that statement. From ECMA-262:

"Native Object
"A native object is any object supplied by an ECMAScript
implementation independent of the host environment. Standard native
objects are defined in this specification. Some native objects are
built-in;others may be constructed during the course of execution of
an ECMAScript program."

Can you imagine the ramifications of IE not allowing native objects to
have methods? :-)

Perhaps you meant host object. But if that were true, most of
Prototype.js would not work at all in IE.

> Generally it's not a good idea to extend native objects because your
> not sure that another JS will not use the same method name !!

Perhaps you did mean host object. Yes, it's a bad idea to add *non-
standard* properties to host objects. How do you reconcile that notion
with the use of Prototype.js's $() function?


--
Rob

david

unread,
Jan 12, 2010, 1:21:51 PM1/12/10
to Prototype & script.aculo.us
Hi RobG,

Thanks to point it out, all should read HOST OBJECT :))

> Can you imagine the ramifications of IE not allowing native objects to
> have methods? :-)

No, I prefer not thinking to that. IE have so much pain for developper
that it didn't need this one.

--
david

Loris Menghi

unread,
Aug 18, 2010, 11:51:29 AM8/18/10
to prototype-s...@googlegroups.com
The fact remains that IE7 does not instantiate the class

T.J. Crowder

unread,
Aug 19, 2010, 1:31:17 AM8/19/10
to Prototype & script.aculo.us
Hi,

The problem is that you have a dangling comma at the end of your
object literal (after the definition of the `srotola` function). IE
considers dangling commas fatal parsing errors; most other browsers
have no problem with them. Here's are two examples of dangling commas:

var obj = {
alpha: 1,
beta: 2, // <== Dangling comma
};

var a = [
"one",
"two", // <== Another dangling comma
];

For your code to work with IE, you have to be sure there is no comma
after the final item in an object literal or an array literal.

Off-topic, but if you're using Prototype 1.6 or above, you're using
Class.create incorrectly. You don't want to do this:

var MyClass = Class.create();
MyClass.prototype = { // <== Incorrect
initialize: function() {
// ...
}
// ...
};

Instead:

var MyClass = Class.create({
initialize: function() {
// ...
}
// ...
});

The two are not identical, Prototype does things with the prototype
that will be lost if you replace it after Class.create is done.

HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com
Reply all
Reply to author
Forward
0 new messages