Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to display html page from string
    primarykey
    data
    text
    <p>I generate some html content dynamically like:</p> <pre><code>var content = "&lt;head&gt;&lt;title&gt;testtitle&lt;/title&gt;&lt;/head&gt;&lt;body&gt;testbody&lt;/body&gt;"; </code></pre> <p>Then I get myself a new tab with <code>about:blank</code>, and now I want to display my generated html in this tab. If the tab's contentDocument is newDoc, I thought I just do:</p> <pre><code>newDoc.documentElement.innerHTML = content; </code></pre> <p>However, that doesn't 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 can't verify, source view remains unchanged.</p> <p>I then tried:</p> <pre><code>newDoc.getElementsByTagName("head")[0].innerHTML = headContent; newDoc.body.innerHTML = bodyContent; </code></pre> <p>Which doesn't change the displayed empty page, also not in the source view, but if I alert newDoc.documentElement.innerHTML, it reflects the changes. It seems like this isn't the document that's displayed any more. Weird.</p> <p>So my question: how do I do that? Specifically in a firefox extension, if that is important.</p> <p>Is there maybe a href format with "text://..." instead of "file://..." or something?</p> <p><strong>Update:</strong></p> <p>It turns out that I can't only replace the full code this way, I can't even body.appendChild, but I'm sure I did that before, so I compared. Here I get my document this way:</p> <pre><code>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; </code></pre> <p>Now I can do:</p> <pre><code>newDoc.location.href = url; </code></pre> <p>And this works, it loads the given page. So I thought this is the correct document, but if I don't assign a url, but instead try to build the dom myself, it simply doesn't work.</p> <p>If I do those changes to window.content.document after the tab is in front, it works. So how come these documents are different? If newDoc is the wrong one, how come assigning a location does anything?</p> <p>Although I can get it to work now, I don't 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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload