[theme-reviewers] Embedding inline JavaScript in <head>?

Otto otto at ottodestruct.com
Tue Oct 16 17:48:48 UTC 2012


On Tue, Oct 16, 2012 at 12:08 PM, Chip Bennett <chip at chipbennett.net> wrote:
> Is there any particular reason that such a script can't be enqueued like
> most other scripts, that would warrant such an exception?

No, but I would still advocate allowing it for small scripts. Define
"small scripts" arbitrarily. Say, less than 8 lines.

The biggest reason to allow it is simply one of speed. Enqueuing
forces you to have the script in a separate file, and adds the
overhead of an extra HTTP request for the most common cases. For a
single line of JS, this is a lot of unnecessary extra overhead.
Especially when the script has no dependencies and is simply a shiv to
do one particular thing. In this case, he's wanting to change no-js
classes to js classes, via js, which makes sense for allowing pure CSS
to handle the no-js case gracefully.

The thinking is along the same lines as the "data URI" scheme, where
you can include binary data like images inline to CSS or HTML as
base64 text. For small files, the setup and teardown and data transfer
overhead of an HTTP connection is quite a bit slower than simply
always serving the file. You lose caching benefits in some cases, but
at a certain point of smallness, it's worth that minor overhead to
eliminate a larger overhead.

-Otto


More information about the theme-reviewers mailing list