greasemonkey and onclick

Discussion in 'Javascript' started by Matej, Mar 2, 2006.

  1. Matej

    Matej Guest

    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
     
    Matej, Mar 2, 2006
    #1
    1. Advertising

  2. 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
    <http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-89647724>
    so as always with JavaScript the case matters.

    > 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
    );


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Mar 2, 2006
    #2
    1. Advertising

  3. Matej

    Matej Guest

    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
     
    Matej, Mar 2, 2006
    #3
  4. Matej

    Matej Guest

    And yes, it was my wrong capitalization of accessKey property.

    Thanks,

    Matej
     
    Matej, Mar 2, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adrian Holovaty

    ANN: Greasemonkey compiler

    Adrian Holovaty, Apr 27, 2005, in forum: Python
    Replies:
    0
    Views:
    369
    Adrian Holovaty
    Apr 27, 2005
  2. why the lucky stiff
    Replies:
    1
    Views:
    222
    Phil Tomson
    Aug 30, 2005
  3. Lachie
    Replies:
    1
    Views:
    104
    Adam Keys
    Mar 24, 2006
  4. Newton
    Replies:
    2
    Views:
    342
    Lasse Reichstein Nielsen
    Sep 21, 2005
  5. Robert Kühne

    instanceof, HTMLSelectElement and Greasemonkey

    Robert Kühne, Apr 22, 2007, in forum: Javascript
    Replies:
    0
    Views:
    102
    Robert Kühne
    Apr 22, 2007
Loading...

Share This Page