473,396 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3090
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

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

Similar topics

3
by: Michael Hill | last post by:
I received an error on the addEventListener line in this function. function load() { el = document.getElementById("t"); el.addEventListener("click", l_func, false); } I am using IE 6.0.
1
by: kenzie.campbell | last post by:
I have a small onkeyup script for a textarea, and I'd like to move it out to a separate file with the addEvent function below. Can someone demonstrate this for me? (My attempts have been...
1
by: philjhanna | last post by:
Hi I'm having a problem applying return false to a div via addEventListener. I'm adding this so that I can drag (click-hold-move) over an image. (Its so that I can add zooming to the image)...
2
by: philjhanna | last post by:
Hi, Does anyone know why I can't add return false with addEventListener in firefox (1.0.6). This demonstrates the problem If return false is added to onmousedown then you can drag over the...
2
by: Terry | last post by:
Hi all, I have been googling this problem for many hours... I have the following greasemonkey script; (function() { if (true) { // Change these to match your setup var sabcomputer =...
2
by: mask | last post by:
example: function disableText() { return false; } document.addEventListener("mousedown", disableText, false); i search many examples that use document.onmousedown = sidableText, so, how...
10
by: Peter Michaux | last post by:
Hi, Today I have been testing the event models from Netscape 4.8 and IE 4 to the current crop of browsers. I'd like to write a small event library similar in purpose to the Yahoo! UI event...
1
by: sylver | last post by:
Hi, Unregistering event listeners (or memory allocation) is a good practice in programming, but do we need to use removeEventListener() for all addEventListener() implementation? Please...
5
by: marchaos | last post by:
I'm trying to override the native HTMLElement.addEventListener in firefox so that I can do some extra things. I know it's possible to add methods to the HTMLElement prototype and for them to be...
0
by: wpjoju | last post by:
i have this code which adds an event listener to a newly opened window but it doesn't seem to work in google chrome. the problem is window.addEventListener seem to work in chrome but if you do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.