greasemonkey and onclick

M

Matej

Hi,

I try to assign my function to one <A> element in this GM script, but
it doesn't work as hoped for (after pressing Alt+L in Firefox 1.5
nothing happens). Does anybody see what I am doing wrong here?

window.openNewLocation = function() {
alert("OK");
}

var ATags = document.getElementsByTagName("a");
var hrefArray = window.location.href.split("/");
hrefArray[hrefArray.length-1] = "gallery";
var imageHREF = hrefArray.join("/");

// window.open(this.href,'extern').focus();return false"
// http://cingular.rbmfrontline.com/locations

for (var i = 0; i < ATags.length; i++)
{
tempElem = ATags;
if (tempElem.getAttribute("href") ==
"http://cingular.rbmfrontline.com/locations") {
tempElem.accesskey = "L";
tempElem.onClick = "function() {openNewLocation();return false}";
GM_log('attributes = ' + String(tempElem.onClick));
}
};

Thanks a lot,

Matej
 
M

Martin Honnen

Matej wrote:

var ATags = document.getElementsByTagName("a");
for (var i = 0; i < ATags.length; i++)
{
tempElem = ATags;
if (tempElem.getAttribute("href") ==
"http://cingular.rbmfrontline.com/locations") {
tempElem.accesskey = "L";


The DOM property is named accessKey, see
tempElem.onClick = "function() {openNewLocation();return false}";

Case matters again, the event handlers in DOM are all lower case so
tempElem.onclick
is needed. Then you would need to assign a function object and not a
string e.g.
tempElem.onclick = function (evt) {
openNewLoation();
if (typeof evt != 'undefined' && typeof evt.preventDefault) {
evt.preventDefault();
}
return false;
};

However as you say you are writing a GreaseMonkey script then scripting
onclick is not going to work, see
<http://diveintogreasemonkey.org/patterns/intercept-clicks.html>
but you should simple do
tempElem.addEventListener(
'click',
function (evt) {
openNewLoation();
evt.preventDefault();
return false;
},
false
);
 
M

Matej

You are partially right -- I have to use addEventListener, but my
script doesn't work. I will ask for more info on GM list.

Thanks,

Matej
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top