views:

409

answers:

2

Hey,

I can easily traverse the current window while I'm inside the pageLoad function in my extension using content.document, but when I try accessing it in another function, called with setInterval, I can't access the content.document.

I tried content.document, document.defaultView, window.content.document and basically every other sensible combination I could think of.

please help. thanks.

A: 

I'm not sure why that wouldn't' work (I'd have to see more code). However, an alternative would be to use gBrowser.selectedBrowser.contentDocument.

sdwilsh
A: 

I got this to work by getting the document object out of an event like so:

   e.target.defaultView.document

The event object is available in any event listener function - for e.g. you may be capturing the document loaded event by doing something like this:

   var appcontent = window.document.getElementById('appcontent');
   appcontent.addEventListener('DOMContentLoaded', onPageLoad, false);

In this example, the onPageLoad function gets an event argument that can be used to fetch the document.

To be able to access the document object elsewhere, set a global variable pointing to that. And if using jQuery, pass this as the optional second argument to the $ function (i.e. the context on which jQuery works).

Gowri