views:

47

answers:

2

I am trying to send the document and the control that the key was pressed in to the keypressed function.

Here is my code:

//Namespace
MyExt.BrowserOverlay = {
  init: function() {
        var appcontent = document.getElementById("appcontent");   // browser
        if(appcontent)
              appcontent.addEventListener("DOMContentLoaded", MyExt.BrowserOverlay.onPageLoad, true);
  },

  onPageLoad: function(aEvent) {
                 var doc = aEvent.originalTarget;
                 if (doc.location.href == "http://something.com"){
                     var txtBox = doc.getElementById('txtBox');
                      txtBox.addEventListener('keypress', keypressed, false); //Error Line
                                }
                 },

something like:


txtBox.addEventListener('keypress', keypressed(?,doc), false);

function keypressed(a,doc){
    alert(a); //a relates to keypress
    alert(doc.innerHTML);
}
A: 

Easiest way to pass variable is to attach it to Element that will trigger event, but you can access document by using global variable document.

As for event listeners, browsers handle events differently:

txtBox.someVar = "foobar"; // Any variable you want to pass

if(window.addEventListener){ // Firefox, Chrome...
 txtBox.addEventListener('keypress', keypressed, false);
} else { // IE
 txtBox.attachEvent('onkeypress', keypressed);
}

 

function keypressed(event){

 // find event object
 var e = event || window.event;

 // find target object
 var target = e.currentTarget;
 if (e.target) target = e.target;
 else if (e.srcElement) target = e.srcElement;
 if (target.nodeType == 3) target = targ.parentNode;

 // find key code
 var code = e.charCode || e.keyCode;

 alert(String.fromCharCode(code));
 alert(target.someVar);
 alert(document.body.innerHTML);

}
draganHR
(document) will not work in this case because multiple tabs are open, and possibly the same pages and elements. I don't want to modify all of them just the one that created the event. Can you return the document from the control that is on it? Unfortunately a global variable might be my only way out of this.
Anon
If you have `doc` variable, pass it like i passed `someVar`.
draganHR
doc is not a global that can be used outside of the onPageLoad function, and keypressed is a private function outside of the MyExt.Browseroverlay namespace. var target = e.currentTarget; //Returns the control not the document.I am trying to get both.
Anon
A: 

You can use gBrowser.contentDocument to get the document of the currently selected tab. See this article on the tabbed browser control for more info.

jeffamaphone