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

Getting a method's name

Here's my situation. I have an object
function Obj () {
this.foo = null;
}

a function
function bar() {...}

another function

function doSomething () {
var obj = new Obj();
obj.foo = bar;
doSomethingElse(obj);
}

In function doSomethingElse I want to create the following line of DHTML

<div onClick="obj.foo(); return true;">
What I want is for onClick to be defined to be the execution of the foo
method of obj.

I tried document.writeln ("<div onClick='" + obj.foo + "(); return
true;'>");
What I get is the source code for bar stuck into the middle of the string.
So how do I get just the name of the function bar in the string? Or
ultimately, how do I get the handler to be bar in the DHTML?

Thanks.

Ken
Jul 23 '05 #1
6 1848
Lee
Ken Kast said:
In function doSomethingElse I want to create the following line of DHTML

<div onClick="obj.foo(); return true;">
What I want is for onClick to be defined to be the execution of the foo
method of obj.

I tried document.writeln ("<div onClick='" + obj.foo + "(); return
true;'>");
There's no trick to it. It's just a string;

document.writeln('<div onclick="obj.foo();return true;">');

What I get is the source code for bar stuck into the middle of the string.
So how do I get just the name of the function bar in the string? Or
ultimately, how do I get the handler to be bar in the DHTML?


document.getElementById("theIdAttributeOfYourDiv") .onclick=obj.foo;

Jul 23 '05 #2
But obj.foo won't be defined when that line of HTML is parsed. I
essentially need to convert the "relative" (to the script) ref to obj.foo to
an "absolute" ref to bar when the browser sees the HTML coming in via the
writeln statement.

Ken
"Lee" <RE**************@cox.net> wrote in message
news:cb********@drn.newsguy.com...
Ken Kast said:
In function doSomethingElse I want to create the following line of DHTML

<div onClick="obj.foo(); return true;">
What I want is for onClick to be defined to be the execution of the foo
method of obj.

I tried document.writeln ("<div onClick='" + obj.foo + "(); return
true;'>");


There's no trick to it. It's just a string;

document.writeln('<div onclick="obj.foo();return true;">');

What I get is the source code for bar stuck into the middle of the string.So how do I get just the name of the function bar in the string? Or
ultimately, how do I get the handler to be bar in the DHTML?


document.getElementById("theIdAttributeOfYourDiv") .onclick=obj.foo;

Jul 23 '05 #3
Lee
Ken Kast said:

But obj.foo won't be defined when that line of HTML is parsed. I
essentially need to convert the "relative" (to the script) ref to obj.foo to
an "absolute" ref to bar when the browser sees the HTML coming in via the
writeln statement.


Then the correct solution depends on what you're actually
trying to accomplish, and probably involves either global
variables.

Jul 23 '05 #4
"Ken Kast" <ke*@NOSPAMkenkast.com> writes:

....
function doSomething () {
var obj = new Obj();
obj.foo = bar;
doSomethingElse(obj);
}

In function doSomethingElse I want to create the following line of DHTML

<div onClick="obj.foo(); return true;">
What I want is for onClick to be defined to be the execution of the foo
method of obj.
Then this won't work.

At this point of the execution, the only reference to the object is
the first parameter of the doSomethingElse function.

What you want to write is a way to reference that value from the global
scope (where the intrinsic event handler is executed).

To do that, you must make a globally available reference to the object.
Say:
window.xyzzy = arg1; // or whatever the parameter is called
document.writeln ("<div onclick='window.xyzzy(); " +
"return true;'>");
I tried document.writeln ("<div onClick='" + obj.foo + "(); return
true;'>");
What I get is the source code for bar stuck into the middle of the string.
Yes. I am not sure you realize that you are building syntax here. There
is no way for the syntax to carry an actual reference to the object. At
best it can contain syntax that evaluates to such a reference, but only
if such a reference is available at all in the scope where the code is
executed.
So how do I get just the name of the function bar in the string?
You don't want the name (it's "bar"). You want a Javascript expression
(syntax) that, when evaluated as an intrinsic event handler, evaluates
to a reference to the existing object.
Or ultimately, how do I get the handler to be bar in the DHTML?


Make a global reference with either a fixed or a computed name,
and then use that in the syntax you write.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #5
Ken Kast wrote:
Here's my situation. I have an object
function Obj () {
this.foo = null;
}

a function
function bar() {...}

another function

function doSomething () {
var obj = new Obj();
obj.foo = bar;
doSomethingElse(obj);
}

In function doSomethingElse I want to create the following line of DHTML

<div onClick="obj.foo(); return true;">
What I want is for onClick to be defined to be the execution of the foo
method of obj.

I tried document.writeln ("<div onClick='" + obj.foo + "(); return
true;'>");
What I get is the source code for bar stuck into the middle of the string.
So how do I get just the name of the function bar in the string? Or
ultimately, how do I get the handler to be bar in the DHTML?

Thanks.

Ken

Have a look at this:

// "The following example uses a small generalised closure based function
// that associates object instances with element event handlers."
// http://jibbering.com/faq/faq_notes/closures.html#clObjI
Jul 23 '05 #6
Actually, I found a simple solution by searching this group. I create
a property bar.name="bar". In my DHTML I use obj.foo.name and it
works hunky-dory.

Ken

Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in message news:<ll**********@hotpop.com>...
"Ken Kast" <ke*@NOSPAMkenkast.com> writes:

...
function doSomething () {
var obj = new Obj();
obj.foo = bar;
doSomethingElse(obj);
}

In function doSomethingElse I want to create the following line of DHTML

<div onClick="obj.foo(); return true;">
What I want is for onClick to be defined to be the execution of the foo
method of obj.


Then this won't work.

At this point of the execution, the only reference to the object is
the first parameter of the doSomethingElse function.

What you want to write is a way to reference that value from the global
scope (where the intrinsic event handler is executed).

To do that, you must make a globally available reference to the object.
Say:
window.xyzzy = arg1; // or whatever the parameter is called
document.writeln ("<div onclick='window.xyzzy(); " +
"return true;'>");
I tried document.writeln ("<div onClick='" + obj.foo + "(); return
true;'>");
What I get is the source code for bar stuck into the middle of the string.


Yes. I am not sure you realize that you are building syntax here. There
is no way for the syntax to carry an actual reference to the object. At
best it can contain syntax that evaluates to such a reference, but only
if such a reference is available at all in the scope where the code is
executed.
So how do I get just the name of the function bar in the string?


You don't want the name (it's "bar"). You want a Javascript expression
(syntax) that, when evaluated as an intrinsic event handler, evaluates
to a reference to the existing object.
Or ultimately, how do I get the handler to be bar in the DHTML?


Make a global reference with either a fixed or a computed name,
and then use that in the syntax you write.
/L

Jul 23 '05 #7

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

Similar topics

0
by: Willoughby Bridge | last post by:
Hi - Having trouble getting a multipart series of forms to move to the next form. The problem line is: <form method="post" enctype="multipart/form-data" action="Data_Form1.php"> If the...
2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
4
by: gimme_this_gimme_that | last post by:
Hi, This is sort of a : How to build a Yes/No dialog box qquestion. Or perhaps a question about getting javascript variables from a pop-up window and processing them on a submit. This is...
0
by: Erik | last post by:
Why isn't my update method getting called? Pasted below is an aspx from a 1.1 application I'm working on. It has two textboxes and a button for inserting data into the database, and a datagrid...
1
by: kamleshsharmadts | last post by:
I am using Ajax with struts in web application. from jsp i am calling a function of ajax.js onclick of a button. code of that call function which calling from jsp given as below:- ...
0
by: Steve | last post by:
Hi All, I have a Python script that uses SOAPpy and I'm outputting all of the methods and info about the parameters... I'm having trouble getting information out of the __init__ parameter. ...
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
21
vikas251074
by: vikas251074 | last post by:
I am getting error while entry in userid field. When user enter his user id, an event is fired immediately and user id is verified using AJAX method. But I am getting error 'Object doesn't support...
7
by: Andrus | last post by:
How to get syntactically correct signature which compiles for code template grneration ? I tried code below but it creates syntactically incorrect signature. Andrus. using System; using...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...
0
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,...

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.