Need a little help. Correct style and functionality

in Script development
Subscribe to Need a little help. Correct style and functionality 3 posts, 2 voices



Bruno Tyndall User

I am just getting started and have trouble understanding why my little test script doesn't work. I have pasted the script (below).

Issues:
1) none of the variations that I have tried work on the "lnk.addEventListener" line work.
2) I'd really like to make that call the function test() which is also included in the code below.

Thanks for your help,
B. Tyndall

// ==UserScript==
// @name test
// @description test
// @include http://*
// @copyright Bruno Tyndall
// ==/UserScript==

var test = function() {
alert("Hello World");
}
var main = function() {
var b = document.getElementsByTagName('body')[0];
var d = document.createElement('div');
d.style.position = 'absolute';
d.style.zIndex = 1000;
d.style.top = '5px';
d.style.right = '80px';
var lnk = document.createElement('a');
lnk.setAttribute('style','font-family:Tahoma;font-size:12pt;font-weight:bold;background-color:yellow;color:black;');
var tn = document.createTextNode('Hello World');
lnk.appendChild(tn);
lnk.setAttribute('href','javascript:void(0);');
lnk.addEventListener('onclick', function(e) { alert('hello'); }, false);
d.appendChild(lnk);
b.appendChild(d);

}

main();

 
Itamar Amith Scriptwright

Try replacing 'onclick' with 'click'. You can call test() from within the anonymous function.

 
Bruno Tyndall User

Thanks. That did the trick.