Package Exports
- current-executing-script
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (current-executing-script) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
currentExecutingScript
Get the currently executing script, regardless of its source/trigger/synchronicity. Similar to HTML5's document.currentScript
but arguably much more useful!
Overview
Loose
This utility comprises a set of behaviors for detecting the currently executing script, which does not comply with the HTML spec's concept of document.currentScript
. However, personally, I find it much more useful!
It can get the script
element that was the source of the nearest (deepest) frame in the call stack (so, the currently executing code), regardless of whether or not said source script is being evaluated synchronously for the first time by the browser.
It also has support for getting the script
element that was the source of the farthest (most shallow) frame in the call stack, regardless of whether or not said source script is being evaluated synchronously for the first time by the browser.
Finally, it has experimental support for getting the script
element or attribute node (e.g. onclick
) responsible for creating the current call stack.
Strict
If you are only interested in getting the currently synchronously evaluating script (i.e. like an HTML spec-compliant polyfill for document.currentScript
), take a look at JamesMGreene/document.currentScript instead.
Browser Compatibility
Forthcoming....
Usage
Nearest
To get the nearest (deepest) script for the current call stack:
var scriptEl1 = currentExecutingScript();
var scriptEl2 = currentExecutingScript.near();
Farthest
To get the farthest (most shallow) scripts for the current call stack:
var scriptEl = currentExecutingScript.far();
IMPORTANT: Note that the accuracy of this may be limited by the allowed stack depth of each browser. For example, Chrome defaults to collecting a maximum of the 10 nearest frames but can be configured to collect more (see Error.stackTraceLimit
and --stack-trace-limit
).
Origin
EXPERIMENTAL!!!
To get the script or attribute node (e.g. onclick
) responsible for creating the current call stack:
var scriptElOrAttrNode = currentExecutingScript.origin();
IMPORTANT: Note that the accuracy of this may be limited by the allowed stack depth of each browser. For example, Chrome defaults to collecting a maximum of the 10 nearest frames but can be configured to collect more (see Error.stackTraceLimit
and --stack-trace-limit
).
Errata
- MDN docs for
document.currentScript
: https://developer.mozilla.org/en-US/docs/Web/API/document.currentScript - Demo using old Gist: http://jsfiddle.net/JamesMGreene/9DFc9/
- Original location, old Gist: https://gist.github.com/JamesMGreene/fb4a71e060da6e26511d