Skip to content

Instantly share code, notes, and snippets.

@afonsomatos
Created January 3, 2015 16:00
Show Gist options
  • Save afonsomatos/fe9b3fbb5f13acddb544 to your computer and use it in GitHub Desktop.
Save afonsomatos/fe9b3fbb5f13acddb544 to your computer and use it in GitHub Desktop.
Get script file location
// Get script file location
// doesn't work for older browsers
var getScriptLocation = function() {
var fileName = "fileName";
var stack = "stack";
var stackTrace = "stacktrace";
var loc = null;
var matcher = function(stack, matchedLoc) { return loc = matchedLoc; };
try {
// Invalid code
0();
} catch (ex) {
if(fileName in ex) { // Firefox
loc = ex[fileName];
} else if(stackTrace in ex) { // Opera
ex[stackTrace].replace(/called from line \d+, column \d+ in (.*):/gm, matcher);
} else if(stack in ex) { // WebKit, Blink and IE10
ex[stack].replace(/at.*?\(?(\S+):\d+:\d+\)?$/g, matcher);
}
return loc;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment