[Greasemonkey] Problem with functions (GM 0.6.4)

esquifit esquifit at yahoo.de
Sun Jan 1 15:51:46 EST 2006


Adding event handlers that way doesn't work any longer.  You have to
use addEventListener instead (see Pitfall #2):

http://www.oreillynet.com/pub/a/network/2005/11/01/avoid-common-greasemonkey-pitfalls.html?page=3

On Sun, Jan 01, 2006 at 04:10:50AM -0500, Randall Wald wrote:
> 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?

-- 
esquifit 


More information about the Greasemonkey mailing list