- From: Jonas Sicking <jonas@sicking.cc>
- Date: Fri, 23 Mar 2012 15:16:18 -0700
On Fri, Feb 3, 2012 at 10:34 PM, Ian Hickson <ian at hixie.ch> wrote: > > On Mon, 16 Aug 2010, Jonas Sicking wrote: >> >> 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>. > > This feature is trivially implementable using the next one, so I haven't > added it. The whole point of this feature is to enable the js-code inside a loaded script to use the .currentScript property in order to find the <script> element in the DOM which loaded it. While it is trivial to implement the .currentScript property from outside of the <script> element, it is impossible to do so from inside the loaded script itself. This since the script runs after the fired event. I just ran into another example of a script which tries to figure out which <script> element loaded it. In this case it does so by checking which <script> element has a src attribute which contains the url it expects to be loaded from. This causes it to fail when I load the script from another url. In this case it was the respec.js script available here: http://dev.w3.org/2009/dap/ReSpec.js/js/respec.js So yes, it's trivial for pages to implement this. But it mostly defeats the purpose if every page which wants to use a given library has to re-implement this property. And it likely will mean that script libraries won't be able to depend on the property since pages won't implement it reliably enough. / Jonas
Received on Friday, 23 March 2012 15:16:18 UTC