Skip to content

Instantly share code, notes, and snippets.

@cphoover
Created August 14, 2013 04:41
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cphoover/6228063 to your computer and use it in GitHub Desktop.
Save cphoover/6228063 to your computer and use it in GitHub Desktop.
document.currentScript polyfill via https://raw.github.com/samyk/jiagra/master/jiagra.js
if("undefined" === typeof document.currentScript){
(function(){
/***************************************************************************/
/* document.currentScript polyfill + improvements */
/***************************************************************************/
var scripts = document.getElementsByTagName('script');
document._currentScript = document.currentScript;
// return script object based off of src
var getScriptFromURL = function(url) {
for (var i = 0; i < scripts.length; i++)
if (scripts[i].src === url)
return scripts[i];
return undefined;
}
var actualScript = document.actualScript = function() {
if (document._currentScript)
return document._currentScript;
var stack;
try {
omgwtf
} catch(e) {
stack = e.stack;
};
if (!stack)
return undefined;
var e = stack.indexOf(' at ') !== -1 ? ' at ' : '@';
while (stack.indexOf(e) !== -1)
stack = stack.substring(stack.indexOf(e) + e.length);
stack = stack.substring(0, stack.indexOf(':', stack.indexOf(':')+1));
return getScriptFromURL(stack);
};
if (document.__defineGetter__)
document.__defineGetter__('currentScript', actualScript);
})();
}
@cowboy
Copy link

cowboy commented Sep 26, 2013

@JamesMGreene
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment