[Greasemonkey] Problem with functions (GM 0.6.4)

Randall Wald randy at rwald.com
Sun Jan 1 04:10:50 EST 2006


If this isn't the right forum in which to ask for scripting help,
please direct me to the correct place.

Anyway, I've got GM 0.6.4, Fx 1.5, Win XP. I'm trying to update the
following script, though it's not really important:
http://userscripts.org/scripts/show/1664

The point is, I'm adding a new link with an onClick attribute. The
method used in the above script (something like link.onclick = foo();
to make <a onclick="foo();">) didn't work, so I used link.setAttribute
("onClick", "foo();"); to make <a onclick="foo();">. However, I found
that if foo() is a function defined elsewhere in my script, the
browser can't see that definition; I get a "not defined" error.
Actually, if instead of foo() I just spell out the script, I still get
not "defined errors" when referring to anything I defined elsewhere in
the script. Here's a base case which weirdly only alerts me with Test1
and not Test2 (link is defined and placed as in the aforementioned
script):

function testFunction(text) { alert(text); }
testFunction("Test1");
link.setAttribute ("onClick", "testFunction('Test2');");

That snippit will pop up Test1 on page load, but when clicking the
link I get "testFunction not defined" errors. What gives?

Sincerely,

Randall Wald


More information about the Greasemonkey mailing list