Scripted event handlers in objects cont'd 
Author Message
 Scripted event handlers in objects cont'd

I've looked through faqts.javascript.com and got some good stuff (and
webreference was good), but not this yet...

Perhaps someone with experience writing objects in client javascript could
help. We know that embedded onclick event handlers in html are good for
embedding arguments to fn calls. When you write an object, though, it seems
you need to use scripted handling- document.myDiv.myElement.onclick =
this.method; since of course without the nam of the instance you can't get
back 'in' to theobject ot call its methods.  Has anyone found a good way to
add args to these scripted handlers? The info might be out of date by the
time you want the arg, ie when the click happens, not when the html is
inserted/embedded. Insights would be appreciated...

Tks
Justin Dutoit



Mon, 28 Jun 2004 08:49:13 GMT  
 Scripted event handlers in objects cont'd
I'm not sure I completely understand what you're trying to do.  You might
consider this:

Note that NS and IE use different implementations of the DIV DOMs, so:

// browser detect code
if (document.all) {
 isIE=true;

Quote:
} else if (document.layers) {
 isNS=true;
} else {
 isOther=true;
}

function myMethod(divname) {
 if (isIE) {
  var before = "document.all.";
  var the_object = before + divname;
  var theIEObj = eval(the_object);
  // place your method code here using theIEObj.method() or
theIEObj.property,
  // etc.
 } else if (isNS) {
  var before = "document.";
  var the_object = before + divname;
  var theNSObj = eval(the_object);
  // place your NS method code here with the theNSObj object.method().
 } else {
  alert("You need to have Netscape 4 or Internet Explorer 4 to view this.");
 }

Quote:
}

Then in your code:
<DIV ID="myDiv" NAME="myDiv"><A HREF="javascript:void(0);" onClick="myMethod
_
  ('myDiv');">Click this to call the method.</A></DIV>

Good luck!

--Robert Paveza

Quote:

> I've looked through faqts.javascript.com and got some good stuff (and
> webreference was good), but not this yet...

> Perhaps someone with experience writing objects in client javascript could
> help. We know that embedded onclick event handlers in html are good for
> embedding arguments to fn calls. When you write an object, though, it
seems
> you need to use scripted handling- document.myDiv.myElement.onclick =
> this.method; since of course without the nam of the instance you can't get
> back 'in' to theobject ot call its methods.  Has anyone found a good way
to
> add args to these scripted handlers? The info might be out of date by the
> time you want the arg, ie when the click happens, not when the html is
> inserted/embedded. Insights would be appreciated...

> Tks
> Justin Dutoit



Mon, 28 Jun 2004 11:18:57 GMT  
 Scripted event handlers in objects cont'd
Thanks for the reply Robert, but that's not what I'm trying to do.
I posted this earlier- these 2 lines work, but I want to pass an arg, and
I'd like to see how
someone gets their arg to plug into this.tester below.

document.all[this.switchScreenName[level]].insertAdjacentHTML("beforeEnd",
html);
   document.all[this.switchScreenName[level]].all['fred0'].onclick =
this.tester;

I tried this- it didn't work anyway. If it did, I'm a bit confused about
whether that function() persists.

document.all[this.switchScreenName[level]].insertAdjacentHTML("beforeEnd",
html);

   document.all[this.switchScreenName[level]].all['fred0'].onclick =
                                function() { this.tester(32); };

You see the general problem. You have an object, so to call its methods on
click, you need to define the handler inside the object, but you need fixed
args.

Tks
Justin



Quote:
> I'm not sure I completely understand what you're trying to do.  You might
> consider this:

> Note that NS and IE use different implementations of the DIV DOMs, so:

> // browser detect code
> if (document.all) {
>  isIE=true;
> } else if (document.layers) {
>  isNS=true;
> } else {
>  isOther=true;
> }

> function myMethod(divname) {
>  if (isIE) {
>   var before = "document.all.";
>   var the_object = before + divname;
>   var theIEObj = eval(the_object);
>   // place your method code here using theIEObj.method() or
> theIEObj.property,
>   // etc.
>  } else if (isNS) {
>   var before = "document.";
>   var the_object = before + divname;
>   var theNSObj = eval(the_object);
>   // place your NS method code here with the theNSObj object.method().
>  } else {
>   alert("You need to have Netscape 4 or Internet Explorer 4 to view
this.");
>  }

> }

> Then in your code:
> <DIV ID="myDiv" NAME="myDiv"><A HREF="javascript:void(0);"
onClick="myMethod
> _
>   ('myDiv');">Click this to call the method.</A></DIV>

> Good luck!

> --Robert Paveza


> > I've looked through faqts.javascript.com and got some good stuff (and
> > webreference was good), but not this yet...

> > Perhaps someone with experience writing objects in client javascript
could
> > help. We know that embedded onclick event handlers in html are good for
> > embedding arguments to fn calls. When you write an object, though, it
> seems
> > you need to use scripted handling- document.myDiv.myElement.onclick =
> > this.method; since of course without the nam of the instance you can't
get
> > back 'in' to theobject ot call its methods.  Has anyone found a good way
> to
> > add args to these scripted handlers? The info might be out of date by
the
> > time you want the arg, ie when the click happens, not when the html is
> > inserted/embedded. Insights would be appreciated...

> > Tks
> > Justin Dutoit



Mon, 28 Jun 2004 11:35:22 GMT  
 Scripted event handlers in objects cont'd
Hi Justin,

I am not sure if your code statement#1 with the
insertAdjacentHTML method was causing error, but for
statement#2, the code below works after a fashion..
----------------------------------------------------------
var tempObj = document.all[this.switchScreenName
[level]].all['fred0'];  

var clickFunc = new Function("var fNew = " + this.tester
+ "; fNew(32);");

tempObj.onclick = clickFunc;
-----------------------------------------------------------

Quote:
>I tried this- it didn't work anyway. If it did, I'm a bit
>confused about
>whether that function() persists.

I think it does, until replaced by another handler. When I
explicitly set the object reference to null, the click
event on my test object still worked.
 hmmm...pity there's no direct way of doing it.

cheers,
 Meenal



Mon, 28 Jun 2004 21:00:30 GMT  
 Scripted event handlers in objects cont'd
Thanks. Would you mind explaining further, especially the line
var clickFunc = new Function("var fNew = " + this.tester
+ "; fNew(32);");
What does new Function(" ...") do?

Ta
Justin


Quote:
> Hi Justin,

> I am not sure if your code statement#1 with the
> insertAdjacentHTML method was causing error, but for
> statement#2, the code below works after a fashion..
> ----------------------------------------------------------
> var tempObj = document.all[this.switchScreenName
> [level]].all['fred0'];

> var clickFunc = new Function("var fNew = " + this.tester
> + "; fNew(32);");

> tempObj.onclick = clickFunc;
> -----------------------------------------------------------

> >I tried this- it didn't work anyway. If it did, I'm a bit
> >confused about
> >whether that function() persists.

> I think it does, until replaced by another handler. When I
> explicitly set the object reference to null, the click
> event on my test object still worked.
>  hmmm...pity there's no direct way of doing it.

> cheers,
>  Meenal



Tue, 29 Jun 2004 09:53:27 GMT  
 Scripted event handlers in objects cont'd

Quote:
>What does new Function(" ...") do?

this link would help:

<http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/script56/html/js56jsobjfunction.asp>

regards,
Meenal



Tue, 29 Jun 2004 12:53:41 GMT  
 Scripted event handlers in objects cont'd

of course the following construct ought to work too -

var theFunc = this.tester;
tempObj.onclick = function()
                  {
                    theFunc(32);
                  };

cheers,
 Meenal



Tue, 29 Jun 2004 22:41:49 GMT  
 Scripted event handlers in objects cont'd
OK, but I was referring to the way you had a string inside = new Function()
{"..." + x + "..")
Regarding this:

var theFunc = this.tester;
tempObj.onclick = function()
                  {
                    theFunc(32);
                  };

isn't that the same as tempObj.onclick = function() { this.myMethod(32); }
?

Thanks
Justin


Quote:

> of course the following construct ought to work too -

> var theFunc = this.tester;
> tempObj.onclick = function()
>                   {
>                     theFunc(32);
>                   };

> cheers,
>  Meenal



Wed, 30 Jun 2004 09:49:59 GMT  
 Scripted event handlers in objects cont'd

Quote:
>Regarding this:

>var theFunc = this.tester;
>tempObj.onclick = function()
>                  {
>                    theFunc(32);
>                  };
>isn't that the same as tempObj.onclick = function() {
>this.myMethod(32); }

Yes it is but its the js namespace thats the key.

When you use "this" after an assignment to the element,
it automatically assumes that "this" should be the element
which is where the problem lies.  However b/c you made a
new var that specifically refers to your object it works.

I do concur that it is kindof a hack to do it this way as
well, but its probably the best way of any.

Sean

Quote:
>-----Original Message-----
>OK, but I was referring to the way you had a string

inside = new Function()
Quote:
>{"..." + x + "..")
>Regarding this:

>var theFunc = this.tester;
>tempObj.onclick = function()
>                  {
>                    theFunc(32);
>                  };

>isn't that the same as tempObj.onclick = function() {

this.myMethod(32); }
Quote:
>?

>Thanks
>Justin



>> of course the following construct ought to work too -

>> var theFunc = this.tester;
>> tempObj.onclick = function()
>>                   {
>>                     theFunc(32);
>>                   };

>> cheers,
>>  Meenal

>.



Wed, 30 Jun 2004 18:54:26 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Problem with reassigning an object's event handlers

2. Scripted event handlers in objects

3. Add an event handler to the actual event handler for an element

4. Determining which object fired event from event handler.

5. Referencing document object of IFrame object in event handler

6. Can't print images - cont'd

7. Trouble with Response.Write 'ing javascript event handlers

8. this object in event handler

9. JScript Event handler for activex object

10. Scripting event handlers for OCX controls

11. margin problems, cont'd - align.ps fails

12. Editing Interleaf PS files cont'd

 

 
Powered by phpBB® Forum Software