469,646 Members | 1,567 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

addEventListener to non-HTML object?

5
What I am unsuccessful at doing is adding the event listener to an object in memory which isn't referencing an HTML element:

Expand|Select|Wrap|Line Numbers
  1. // DATA OBJECT CONSTRUCTOR
  2. function dataObject(id){ 
  3.  
  4.     //  EVENT
  5.     this.addEventListener("error",function(event){this.err(event)},true);
  6.  
  7.     // METHOD
  8.     this.err = function(e){
  9.         // ERROR HANDLING HERE
  10.     }
  11.  
  12. }
  13.  
I get this error in my error console:

this.addEventListener is not a function

Perhaps I don't fully understand the exact behavior of events and which objects can raise them.

I am able, however, to use addEventListener to an HTML object. This works fine:

Expand|Select|Wrap|Line Numbers
  1. // DIV OBJECT CONSTRUCTOR
  2. function divObject(id){ 
  3.  
  4.     // ELEMENT
  5.     var div = document.getElementById(id);
  6.  
  7.     //  EVENT
  8.     div.addEventListener("error",function(event){div.err(event)},true);
  9.  
  10.     // METHOD
  11.     this.err = function(e){
  12.         // ERROR HANDLING HERE
  13.     }
  14.  
  15. }
  16.  
My aim is to add an event to an object, and trap errors if they should occur in the code blocks inside. Is this possible?
Oct 1 '07 #1
3 2930
mrhoo
428 256MB
In any language, this.someMethod() will only function if the object of this has a someMethod method defined for it. You need to define the method, or co-opt a method of an existing object, by reference.
Oct 1 '07 #2
Greg A
5
In any language, this.someMethod() will only function if the object of this has a someMethod method defined for it. You need to define the method, or co-opt a method of an existing object, by reference.
Thanks for the quick reply.

If I hear you correctly, I must first write a method using this.methodName before I can call it...hence the "this.addEventListener is not a function" error. It's treating "addEventListener" as a user-defined method in my object and assumes it's a reference error.

Ok, so moving on from here...is there a way to add an event listener to an object so I can raise events with the onerror event type?

The purpose would be for detecting errors inside of objects without enclosing the entire code block with a try/catch.

If this is possible, error reporting/suppression would be much easier - since I would be able to report the name of the object from which the onerror event was fired with arguments.callee.name. And, it would allow me to suppress any/all errors within the object, and it would simply die without kicking and screaming.

Maybe I'm dreaming too big here.
Oct 1 '07 #3
dmjpro
2,476 2GB
What I am unsuccessful at doing is adding the event listener to an object in memory which isn't referencing an HTML element:

Expand|Select|Wrap|Line Numbers
  1. // DATA OBJECT CONSTRUCTOR
  2. function dataObject(id){ 
  3.  
  4.     //  EVENT
  5.     this.addEventListener("error",function(event){this.err(event)},true);
  6.  
  7.     // METHOD
  8.     this.err = function(e){
  9.         // ERROR HANDLING HERE
  10.     }
  11.  
  12. }
  13.  
I get this error in my error console:

this.addEventListener is not a function

Perhaps I don't fully understand the exact behavior of events and which objects can raise them.

I am able, however, to use addEventListener to an HTML object. This works fine:

Expand|Select|Wrap|Line Numbers
  1. // DIV OBJECT CONSTRUCTOR
  2. function divObject(id){ 
  3.  
  4.     // ELEMENT
  5.     var div = document.getElementById(id);
  6.  
  7.     //  EVENT
  8.     div.addEventListener("error",function(event){div.err(event)},true);
  9.  
  10.     // METHOD
  11.     this.err = function(e){
  12.         // ERROR HANDLING HERE
  13.     }
  14.  
  15. }
  16.  
My aim is to add an event to an object, and trap errors if they should occur in the code blocks inside. Is this possible?

Use try catch blocks.
Expand|Select|Wrap|Line Numbers
  1. try{
  2. //some code
  3. }catch(e){
  4. //some code
  5. }
  6.  
Good Luck!

Kind regards,
Dmjpro.
Oct 2 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Michael Hill | last post: by
1 post views Thread by kenzie.campbell | last post: by
2 posts views Thread by Terry | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.