Scripts imported from templates in other documents that got there via an innerHTML setter do not run




DOM: Core & HTML
a year ago
a year ago


(Reporter: matthew, Unassigned, NeedInfo)


47 Branch

Firefox Tracking Flags

(Not tracked)



(3 attachments)

629 bytes, application/zip
22 bytes, application/x-javascript
605 bytes, text/html


a year ago
Created attachment 8772840 [details]

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36

Steps to reproduce:

1. Created a new document using document.implementation.createHTMLDocument
2. Created a template inside that document with a script.
3. Imported that template into the main document using document.importNode
4. Gave the script a src and inserted it into the main document.

Actual results:


Expected results:

The script should have executed and the script's onload event should have fired.

Comment 1

a year ago
Created attachment 8773029 [details]

Comment 2

a year ago
Created attachment 8773031 [details]


a year ago
Component: Untriaged → DOM: Core & HTML
Product: Firefox → Core
Boris, is this a bug in Gecko?
Flags: needinfo?(bzbarsky)
Hmm.  So the relevant spec here is starting at which calls into which calls into which starts running the parser.  We see the <template> tag, enter then see the <script> tag and process at as if in the "in head" insertion mode, per spec.  That lands us in where we see the <script> start tag token and run the relevant steps.  Step 4 is:

  If the parser was originally created for the HTML fragment parsing algorithm,
  then mark the script element as "already started". (fragment case)

So after this point this script will never execute.  So as far as I can tell, we're doing exactly what the spec says here.  Anne, am I missing something?
Flags: needinfo?(bzbarsky) → needinfo?(annevk)
Summary: Scripts imported from templates in other documents do not load → Scripts imported from templates in other documents that got there via an innerHTML setter do not run

Comment 5

a year ago
Yeah, this looks like a bug in Chrome. No other browser does this.
Flags: needinfo?(annevk)
Marking invalid.  I went to file a Chromium issue on this, then discovered one has been filed since late 2015 and there is a patch under review as of a week or two ago.  See
Last Resolved: a year ago
Resolution: --- → INVALID

Comment 7

a year ago
Thanks for the explanation. This doesn't seem intuitive though, this means you can't have scripts within templates? I thought the point of templates was to template things.
You can have <script> within templates.  What you can't do is use the innerHTML setter to get runnable scripts.

Just like you can have <div><script></script></div> and the script will run, but if you start with an empty <div> and set its innerHTML to "<script></script>" the script will NOT run.  That is, the weird thing here is innerHTML, not templates.

Comment 9

a year ago
Ah, so the problem is where I set the new document's body. I did it this way because it emulates what the HTML Imports polyfill does, so there's a bug in that polyfill.

Hmm.  Which polyfill is this?  Sounds like we should work with the authors to get it fixed...
Flags: needinfo?(matthew)

Comment 11

a year ago

Here is where they set the innerHTML:

I filed a bug with them:
You need to log in before you can comment on or make changes to this bug.