Created
January 3, 2015 16:00
-
-
Save afonsomatos/fe9b3fbb5f13acddb544 to your computer and use it in GitHub Desktop.
Get script file location
This file contains 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
// 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