W3C home > Mailing lists > Public > whatwg@whatwg.org > August 2010

[whatwg] <script> features

From: Jonas Sicking <jonas@sicking.cc>
Date: Mon, 16 Aug 2010 21:15:35 -0700
Message-ID: <AANLkTin3t5ZB4DrUxJ8WS_hiuSgs3pMoZL8WOgRTCkfq@mail.gmail.com>
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
Received on Monday, 16 August 2010 21:15:35 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 16:59:26 UTC