0

In a Firefox bootstrapped addon at a stage that window object is not available yet, I would like to create an element to be kept as a template (not to be inserted at the time).

I was wondering if that is possible?
Can createElement() be created anywhere else other than in a window.document?

erosman
  • 7,094
  • 7
  • 27
  • 46
  • Because it has to be created multiple times and I can clone the template instead of creating a new one each and every time. – erosman Jul 01 '14 at 18:19
  • It is already a function and I am not sure what you have mentioned applies here. That stage is the browser start up, before any windows, documents are loaded. – erosman Jul 01 '14 at 18:24

2 Answers2

3

I personally but my stuff in an XML file (a XUL file, really) and use XHR to parse it (.responseXML). The DOMParser via nsIDOMParser is another alternative (although doing sync file I/O during startup isn't a great idea, UX-wise...)

If you want a blank document, IIRC (?!) you can use nsIDocumentLoaderFactory.createBlankDocument.

Then you can .cloneNode(true)/.importNode from the pre-parsed DOM into (browser) windows.

nmaier
  • 32,336
  • 5
  • 63
  • 78
  • Thanks Nils. That was the general idea. I routinely make a template and use `cloneNode()` in normal situations ie JavaScript?GreaseMonkey. I was thinking about the same here. XHR+Parser seems like too much work. addon starts in `sandbox` object. Is it possible to `createElement()` in `sandbox`? – erosman Jul 01 '14 at 19:09
  • 1
    A sandbox does not come with DOM stuff (unless you explicitly inject a `window` or other stuff into it yourself). And you don't need XHR + Parser, just XHR *or* Parser. – nmaier Jul 01 '14 at 19:12
2

You can do Services.appShell.hiddenDOMWindow.document.createElement

But on start of browser you have to add a event listener to listen to when hiddenDOMWindow is loaded.

But doing createElement is same overhead as createElement I'm pretty sure right? So you only need to createElement when need to insert, which will obviously have a window.

Blagoh
  • 1,225
  • 1
  • 14
  • 29
  • 1
    Thank you Blagoh. I think there is a typo. Did you mean "doing `createElement` is same overhead as `cloneNode`"? If that is the case, no, `clonNode` uses a lot less overheads. :) – erosman Jul 02 '14 at 03:44
  • 2
    Well, the big drawback with `.hiddenDOMWindow.document` is that on some platforms it will be a XUL document and on other platforms it will be a HTML document. This dictates the default XML namespace that will be used when `.createElement()` (so you better use `.createElementNS()`). – nmaier Jul 02 '14 at 08:57
  • I didn't know that about `cloneNode` overhead, thanks @erosman ill have to look that up. – Noitidart Jul 02 '14 at 15:39
  • Please share when you find resource on overhead comparison. I would like to see so myself. – Blagoh Jul 02 '14 at 17:26