Skip to content

Instantly share code, notes, and snippets.

@tunderdomb
Created June 25, 2014 14:56
Show Gist options
  • Save tunderdomb/7fc17743b66035946642 to your computer and use it in GitHub Desktop.
Save tunderdomb/7fc17743b66035946642 to your computer and use it in GitHub Desktop.
document.currentScript polyfill (a really barebones one)
if ( typeof document.currentScript == "undefined" && document.__defineGetter__ ) {
document.__defineGetter__("currentScript", function (){
try {
throw new Error()
}
catch ( e ) {
var qualifiedUrl = location.protocol + "//" + location.host
, srcs = e.stack.match(new RegExp(qualifiedUrl + ".*?\\.js", 'g'))
, src = srcs[srcs.length - 1]
, absoluteUrl = src.replace(qualifiedUrl, "")
, scripts = document.scripts
, i = -1
, l = scripts.length
while ( ++i < l ) {
if ( scripts[i][0] == "/" && scripts[i].src == absoluteUrl || scripts[i].src == src ) {
return scripts[i]
}
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment