469,353 Members | 2,265 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 1698
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Willoughby Bridge | last post: by
reply views Thread by Steve | last post: by
2 posts views Thread by karinmorena | last post: by
7 posts views Thread by Andrus | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.