473,725 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to refer to "this" in JS event handler?

Hi.

I am using Atlas and hooking a load event using add_load(...).

This is being called from the constructor of an object being created, so it
is within the context of an object (where 'this' is recognized). I want the
handler to be an instance method on the object instance.

Sys.Application .add_load( this._onLoad ) );

The problem is that when _onLoad gets called, the "this" context does not
refer to my actual object. How can I get access to the object instance
within the handler?
Mar 30 '07 #1
1 1190
use closure when declaring the delegate. have the onLoad function accept
a parameter that is the original object, then declare the delegate as:
Sys.Application .add_load( function(){this ._onLoad(this); } );
-- bruce (sqlwork.com)

Peter Rilling wrote:
Hi.

I am using Atlas and hooking a load event using add_load(...).

This is being called from the constructor of an object being created, so it
is within the context of an object (where 'this' is recognized). I want the
handler to be an instance method on the object instance.

Sys.Application .add_load( this._onLoad ) );

The problem is that when _onLoad gets called, the "this" context does not
refer to my actual object. How can I get access to the object instance
within the handler?

Mar 30 '07 #2

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

Similar topics

1
10273
by: Christoph | last post by:
newFieldElement = document.createElement( 'INPUT' ); newFieldElement.onblur = new Function( "calculatePremiumOptionTotal( this );" ); In my (dynamically generated) javascript, I've never had a problem with attaching a function to an event using the above. However, what's going on with the above is that the reference to the element object (this) isn't being passed to my function. When I do an alert on the argument within the function,...
1
1458
by: lawrence | last post by:
I'm trying to gain a better understanding of javascript by studying examples. I noticed this in an online tutorial. I don't get the use of "this". >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> You might want to add some event handler for an event to an element to which there is already an event handler attached. That is possible by storing the old event handler somewhere and then writing a new function which calls the old event handler.
3
1749
by: Oliver Saunders | last post by:
can someone please tell me why everytime i try and pass this to a js function i can't seem to use it for anything: <html> <head> <script language="javascript"> function a(fromlink) { alert(fromlink.nodeName); // outputs undefined alert(fromlink.nodeType); // outputs undefined alert(fromlink.parentNode); // outputs undefined
7
2005
by: Shapper | last post by:
Hello, I have an ASP:ImageButton where I want to call a function and pass a string: OnClick="Change_Photo("John")" I am having problems with "". I tried
2
1561
by: Bryan | last post by:
Hello all, Can anyone explain when one should use the "document" object and when one should use the "this" object? Also, is the "self" object the same as the "document" or "this" object?
1
1777
by: studen771 | last post by:
Thanks in advance to anyone who can help :): I have an .aspx page that creates javascript on the fly within a button click event handler. The script creates a function AND CALLS THAT SAME FUNCTION that opens a new window which will contain another .aspx page. The new popup window opens just fine, however, the parent page from which the 'click' event originated from produces the error "The resource cannot be found" for some reason. Anyone
5
10460
by: sunsande | last post by:
Hi there! Suppose you have N checkboxes on a MSAccess form and you want that every from them to show it's name on it's OnClick event. So you do: Private Sub checkbox1_Click() MsgBox checkbox1.Name End Sub In other programming languages I would write
2
3495
Plater
by: Plater | last post by:
I am having trouble with my script in IE(IE6) and how it treats a "this". I am thinking it might have something to do with my event wrapper function(see code) but would like to keep that wrapper. function BuildIt() { var taA=document.getElementById("taArea"); var soA=document.getElementById("soArea"); if((taA!=undefined)&&(soA!=undefined)) { for(var i=0;i<NumberOfBoxes;i++)
0
8872
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9392
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9246
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9162
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9091
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8069
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5997
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3211
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2150
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.