Leave out the onclick attribute and use addEventListener <https://developer.mozilla.org/en/DOM/element.addEventListener>.
Example:
    var elem = /* the <a> element in question */;
    elem.href = "javascript:void(0)";
// Note that this does not pass the listener as *text*, but as a *function object* :) elem.addEventListener("click", function (evt) {return myScript.myFunction('text');}, false); // Depending on where the previous line executes, you may not need the myScript qualifier

Some deep background is given on http://wiki.greasespot.net/Security; the gist of it as applicable to your situation is that GM scripts run in a different context for security reasons, and there is no way for the eval <https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/eval>() call implicitly used by the onclick attribute to resolve the object correctly, since it's just not in scope at the time. The solution is to use closures <https://developer.mozilla.org/en/JavaScript/Guide/Closures> to capture a reference to the object while attaching the event listener.

Make sense?

On 2010-08-29 18:53, Dave Clark wrote:
I tried searching the group and even found a question which matches
what I want to do -- but there was no reply to that question.  I hope
I don't get the same result.   ;-)

I have a user.js script which is modifying a link on a page from this:

<a href="...url...">...text...</a>

to this:

<a href="javascript:void(0)" onclick="return
myScript.myFunction('text')">...new text...</a>

Now, "myScript" is the main object in my user.js file.  The problem is
that I get the following error message when I click on the modified
link:

Error: myScript is not defined

How do I get that link to invoke a function in my user.js file?
Thanks.
--
cc | pseudonymous |<http://carlclark.mp/>


--
This is a test of the emergency signature system. Were this an actual signature, you would see amusing mottos, disclaimers, a zillion net addresses, or edifying philosophical statements.

However, this is only a test.  «= tagzilla.mozdev.org

--
You received this message because you are subscribed to the Google Groups 
"greasemonkey-users" group.
To post to this group, send email to greasemonkey-us...@googlegroups.com.
To unsubscribe from this group, send email to 
greasemonkey-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/greasemonkey-users?hl=en.

Reply via email to