How can I assign event handlers externaly?

Discussion in 'Javascript' started by glevik, Apr 15, 2005.

  1. glevik

    glevik Guest

    Hello,

    I have written a script that gets trigered by IE's toolbar button. In
    this script I would like to asign an event handler to an element of the
    document currently open in IE. The way one access the document object
    from a toolbar button script is:

    var doc = external.menuArguments.document;

    Now assuming the document has an element called TextArea1, the logical
    thing to do would be:

    parentwin.document.all('TextArea1').onkeypress = new
    Function('window.alert(\'asdf\');');

    Which goes compiles and runs, except the event handler does not get
    triggered.

    Another trick i tried is as follows:

    var s = parentwin.document.createElement('script');
    s.text = 'window.alert(\'asdf\')';
    s.htmlFor = 'TextArea1';
    s.event = 'onclick';
    parentwin.document.scripts[0] = s;

    Same result.

    I would appreciate any suggestions you might have.

    Lev
     
    glevik, Apr 15, 2005
    #1
    1. Advertisements

  2. ~~~~~~~~~~~~~~~~~~
    Shouldn't that be "doc"?
    I would try it this way (untested quickhack):

    var myWindow, myDocument, myElement;
    if ((myWindow = external.menuArguments)
    && (myDocument = myWindow.document)
    && (myElement = myDocument.all('TextArea1'))
    ) {
    myElement.onkeypress = function() {
    myWindow.alert('asdf');
    };
    }

    ciao, dhgm
     
    Dietmar Meier, Apr 15, 2005
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.