views:

346

answers:

3

Hi all,

I've spent a long time trying different things to get this to work but nothing does and documentation is not helping much.

I'm trying to populate a form inside an iframe that I dynamically inject into a page. To inject that iframe I do:

myObject.iframe = document.createElement("iframe");
myObject.iframe.setAttribute("src", data.url);
myObject.iframe.setAttribute("id","extension-iframe");
myObject.window.document.getElementById('publisher').appendChild(myObject.iframe);
myObject.iframe.addEventListener("load", function(){
    myObject.populate(data);
}, false);

which works fine and DOES trigger the populate() method. My problem is getting the document or window objects for that iframe. I've tried all of the following:

myObject.iframe.window
myObject.iframe.document
myObject.iframe.content

but these are all undefined.

I also tried passing the event object to that iframe's load event listener and then doing:

event.originalTarget

But that didn't work either.

I am reading the following documentation:

But either I'm not understanding it or it's just not properly explained.

Can anyone shed some light?

Thanks!

+2  A: 

Have you tried using the contentWindow property?

var doc = iframe.contentWindow.document;
Sam C
I did but still cannot access the window. Check this out for a more complete explanation: http://gist.github.com/242927And thanks in advance for all the help.
luisgo
I got nothing else then sorry, not familiar with XUL.
Sam C
A: 

I almost had the same problem. Using the following code gets me the iframe (Firebug is used for debugging):

iframes = window.content.document.getElementsByTagName('iframe')
for (var i = 0; i < iframes.length; i++) {
  var elmInput = iframes[i];
  Firebug.Console.log(elmInput);
}
Thariama
A: 

Try myObject.iframe.contentDocument

Something along the lines of this:

// For accessing browser window from sidebar code.
var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                   .getInterface(Components.interfaces.nsIWebNavigation)
                   .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
                   .rootTreeItem
                   .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                   .getInterface(Components.interfaces.nsIDOMWindow);

var gBrowser = mainWindow.gBrowser;

var iframes = gBrowser.contentDocument.getElementsByTagName("iframe");
            for (var i=0; i < iframes.length; i++) {
                var check_elem = iframes[i].contentDocument.getElementById('myid');
                if (check_elem) {
                    ...;
                }
            }
Ivan Kruchkoff