[whatwg] <script> features

Jonas Sicking jonas at sicking.cc
Mon Aug 16 21:15:35 PDT 2010

Hi All,

I'd like to propose a couple of simple features to make <script>
elements more useful:

1. document.currentScript

This property returns the currently executing <script>, if any.
Returns null if no <script> is currently executing. In the case of
several nested executing <script>s, it returns the innermost one. This
is useful for being able to pass parameters to the script by setting
data- attributes on the script element.

I think jQuery already does things that requires knowing which
<script> element linked to jQuery, and it approximates this property
by getting the last element in
document.getElementsByTagName("script"), which won't work reliably.
Especially with features like <script async>.

2. scriptwillexecute/scriptdidexecute events

These events fire right before and right after a <script> is executed.
This allows a page to override the context a specific script is
seeing. For example installing compatibility shims for older browsers.
Another possible use for this is to make ads execute asynchronously.
Currently this is problematic because a lot of ads use document.write
and so the content will be lost (or worse) if an async attribute is
added to the <script> element used to load the ad. Using these events
a page can override document.write while a specific script is
executing and insert the written content into the DOM.

(I've written an implementation for firefox for these features here:

/ Jonas

More information about the whatwg mailing list