473,382 Members | 1,583 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,382 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 1846
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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...

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.