Odd problem with this.getAttribute

43 views
Skip to first unread message

Patrick Wiseman

unread,
Nov 21, 2009, 1:26:39 PM11/21/09
to greasemonkey-users
Hello GM'ers:

I'm having a funny problem with this.getAttribute. Here's a couple of
code snippets:

function doWhatever() {
def=this.getAttribute('id');
}

This, as expected, assigns the value of 'id' to def.

function doWhatever(abc) {
abc ? def=abc : def=this.getAttribute('id');
}

If abc is present, this assigns abc to def; but if abc is absent, it
assigns [objectXPCNativeWrapper[objectMouseEvent]] to def (the
this.getAttribute comes from a mouse click on the element with the
'id' attribute). Any other combination does the same thing -
def=this.getAttribute('id'), which works by itself, breaks the moment
I try to assign it only if abc is absent. I have the feeling I'm
missing something obvious, but I'm trying to write ONE function which
will behave the same way if passed a value or if getting a value from
a clicked element.

Any thoughts would be appreciated.

pw

Patrick Wiseman

unread,
Nov 21, 2009, 1:57:00 PM11/21/09
to greasemonkey-users
try { def=this.getAttribute('id'); } catch(e) { def=abc; } does what I
want, but it doesn't seem right!

pw

cc

unread,
Nov 21, 2009, 3:57:06 PM11/21/09
to greasemon...@googlegroups.com
On 2009-11-21 10:26, Patrick Wiseman wrote:
> Hello GM'ers:
>
> I'm having a funny problem with this.getAttribute. Here's a couple of
> code snippets:
>
> function doWhatever() {
> def=this.getAttribute('id');
> }
>
> This, as expected, assigns the value of 'id' to def.
>
> function doWhatever(abc) {
> abc ? def=abc : def=this.getAttribute('id');
> }
>
> If abc is present, this assigns abc to def; but if abc is absent, it
> assigns [objectXPCNativeWrapper[objectMouseEvent]] to def (the
> this.getAttribute comes from a mouse click on the element with the
> 'id' attribute).
Is this function an event handler, registered with addEventListener? If
so, then I suspect that abc is not actually absent, but is being
populated with the event parameter. Try adding an additional (first)
parameter to catch the event and do whatever with it -- e.g.

function doWhatever(evt, abc) {

For example, you could use evt.target.id instead of this.getAttribute, I
think.

Patrick Wiseman

unread,
Nov 21, 2009, 4:09:26 PM11/21/09
to greasemon...@googlegroups.com
On Sat, Nov 21, 2009 at 3:57 PM, cc <carl...@lavabit.com> wrote:
> On 2009-11-21 10:26, Patrick Wiseman wrote:
>> Hello GM'ers:
>>
>> I'm having a funny problem with this.getAttribute.  Here's a couple of
>> code snippets:
>>
>> function doWhatever() {
>>    def=this.getAttribute('id');
>> }
>>
>> This, as expected, assigns the value of 'id' to def.
>>
>> function doWhatever(abc) {
>>    abc ? def=abc : def=this.getAttribute('id');
>> }
>>
>> If abc is present, this assigns abc to def; but if abc is absent, it
>> assigns [objectXPCNativeWrapper[objectMouseEvent]] to def (the
>> this.getAttribute comes from a mouse click on the element with the
>> 'id' attribute).
> Is this function an event handler, registered with addEventListener? If
> so, then I suspect that abc is not actually absent, but is being
> populated with the event parameter. Try adding an additional (first)
> parameter to catch the event and do whatever with it -- e.g.
>
> function doWhatever(evt, abc) {
>
> For example, you could use evt.target.id instead of this.getAttribute, I
> think.

I bet you're right. I'll mess around with it some more and see if I
can fix it. Thanks.

pw
Reply all
Reply to author
Forward
0 new messages