Created
August 14, 2013 04:41
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
})(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FWIW, samyk/jiagra#2