Calling a tampermonkey function from a link
call, function, hyperlink, javascript, tampermonkey
Solution
Don't set javascript handlers that way. Use `addEventListener()`, like so:
var aNode = document.createElement ('a');
var aText = document.createTextNode ('will it run');
aNode.href = '#';
aNode.appendChild (aText);
document.body.insertBefore (aNode, document.body.firstChild);
aNode.addEventListener ("click", runTest, false);
function runTest (zEvent) {
zEvent.preventDefault ();
zEvent.stopPropagation ();
alert('it ran!');
};
Problem
How can I call a tampermonkey function from a link? Here's what I tried. Using tampermonkey, I can insert a link as follows: ``` var aNode = document.createElement('a'); var aText = document.createTextNode('will it run'); aNode.appendChild(aText); aNode.href = 'javascript:runTest();'; document.body.insertBefore(aNode, document.body.firstChild); function runTest() { alert('it ran!'); }; ``` When the link is called, the function, runTest(), should be called. It isn't. Instead the following error message occurs: Uncaught ReferenceError: runTest is not defined