views:

83

answers:

0

hi.

i generate some html content dynamically like

var content = "<head><title>testtitle</title></head><body>testbody</body>";

then i get myself a new tab with about:blank, and now i want to display my generated html in this tab. if the tab's contentDocument is newDoc, i thought i just do

newDoc.documentElement.innerHTML = content;

but that doesnt work, it seems to have no effect at all. in firebug it seems to work once but screws up firebug at the same time so i cant verify, source view remains unchanged.

i then tried

newDoc.getElementsByTagName("head")[0].innerHTML = headContent;
newDoc.body.innerHTML = bodyContent;

which doesnt change the displayed empty page, also not the source view, but if i alert newDoc.documentElement.innerHTML, it reflects the changes. seems like this isnt the document that's displayed any more. weird.

so my question: how do i do that? specifically in a firefox extension, if that is important. is there maybe a href format with "text://..." instead of "file://..." or something?

thanks.

edit: it turns out that i cant only replace the full code this way, i cant even body.appendChild. but i'm sure i did that before, so i compared. here i get my document this way:

    var tab = getBrowser().addTab(); //make new tab
    getBrowser().selectedTab = tab;  //bring it to front
    var browser = getBrowser().getBrowserForTab(tab); //get the window of the tab
    var newDoc = browser.contentDocument;   

now i can do

newDoc.location.href = url;

and this works, it loads the given page. so i thought this is the correct document. but if i dont assign an url but try to build the dom myself, it simply doesnt work. if i do those changes to window.content.document after the tab is in front, it works. so how come these documents are different? and if newDoc is the wrong one, how come assigning a location does anything? although i can work now, i dont particularly like the solution of getting the document by bringing the tab to the front and then grabbing the window.content document, that feels like a hack and depends on timing.