473,396 Members | 1,832 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.

Event handlers, OO and function context

Hi,
I am writing a javascript code that parses dom and finds event
handlers attached to mouseover events. Then i will replace the
existing handler say B() with my own function say A(). When the event
happen and control comes to my function A(), after doing required
processing i will call B() as shown below

<a href = "abc.com" mouseover = "B();"link </a>

while parsing i will have (trimmed down version)

var oldHandler = node.onmouseover;
node.onmouseover = A;

function A()
{
/ * my code */
oldHandler.call(this);
}

This was working fine as long as B() was a global function. I started
getting problems when B was a member function. For eg:

function Alerter(text)
{
this.text=text;
var me=this;
this.invoke=function ()
{
alert(this.text);
}
}
var sayHi = new Alerter('Hello, world!');

The web developer would have code like
<a href = "abc.com" mouseover = "sayHi.invoke()"link </a>

But this time around, my function A() fails since although i have
handler to sayHi.invoke(), it has to be executed in correct context.
Other wise "this.text" is giving me error because when i say
oldHandler.call(this), i am executing the sayHi.invoke() with the html
element being passed as this.

Is there any way to get the pointer to the object 'sayHi' from
'sayHi.invoke()' as i think that will help me ? Or could any of you
have any better suggestion on this matter . I cannot all the web
developers in world to use the variable "me" so that my code works. :)

Thanks for Reading
Kiran.

Sep 11 '07 #1
2 1206
pr
wolverine wrote:
Perhaps you didn't read/understood my question completely. The web
page can be developed by any one. I have no control over who/how it is
developed. I have already told this at the end of my question.

I am looking for a solution which could work even if the web developer
uses alert(this.text);
Maybe you have to explain it again. If it's not sufficient to call your
own event handler before (optionally) calling the original event handler
then what?
Sep 12 '07 #2
On Sep 12, 6:47 pm, pr <p...@porl.globalnet.co.ukwrote:
wolverine wrote:
Perhaps you didn't read/understood my question completely. The web
page can be developed by any one. I have no control over who/how it is
developed. I have already told this at the end of my question.
I am looking for a solution which could work even if the web developer
uses alert(this.text);

Maybe you have to explain it again. If it's not sufficient to call your
own event handler before (optionally) calling the original event handler
then what?
Let me make things clear. My second post was only to the guy named
"pr" and not to "intrader". This is since "pr" asked me to use
"me.text" instead of "this.text". I think "intrader" has got the
question correct and he has suggested a solution. Thanks for that.

But my problem is i am using IE and not Mozilla. So i think i cannot
really have my handler executing before the actual handler. Or is
there any way in IE to get my handler executing before the actual
handler (of course with out actually using "=" to replace the old
handler with mine)?

My preferred solution would be some thing like i have suggested in my
first post (question itself). I mean a way of getting a pointer to
"sayHi" from handler to "sayHi.invoke()". But let it be clear that i
would be more than happy with any solution.


Sep 12 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Albert Wagner | last post by:
What is the evaluation context of the setTimeout args below? I have a separate Timer instance for each sprite in my program. As coded, "this.Clock" doesn't work. Thanks ahead for any advice. ...
10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
6
by: Amir Hardon | last post by:
I am dynamically adding rows to a table, and each row have a button which removes it. I have successfully implemented this for mozilla but I'm having troubles with IE, here is how I did it: ...
17
by: abs | last post by:
My element: <span onclick="alert('test')" id="mySpan">test</span> Let's say that I don't know what is in this span's onclick event. Is it possible to add another action to this element's onclick...
6
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to...
2
by: pagates | last post by:
Hi All, I have an issue where I'd like a single global event to be handled by multiple event handlers. In this case, I would like a "global" (static) Menu Item that I create in a class, and I'd...
2
by: Safalra | last post by:
Recently I've rewritten much of my old Javascript code to use DOM functions so that enhancements can be attached to documents without needing to alter the HTML code. I assumed that adding event...
4
by: eggie5 | last post by:
I have this even handler (using prototype.js): showCommentsLinks.observe('click', function(event) { alert('hi') }); It's attaching to a link element: <a id="showCommentsLink"...
2
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.