Skip to content

Instantly share code, notes, and snippets.

@eligrey
Last active September 2, 2019 20:34
Show Gist options
  • Save eligrey/5426730 to your computer and use it in GitHub Desktop.
Save eligrey/5426730 to your computer and use it in GitHub Desktop.
Get the current script URL
/* Script location finder
* 2014-04-30
*
* By Eli Grey, http://eligrey.com
* License: X11/MIT
*/
/*! @source https://gist.github.com/eligrey/5426730 */
var current_script_location = (function() {
var
filename = "fileName"
, stack = "stack"
, stacktrace = stack + "trace"
, loc
, matcher = function (stack, matchedLoc) {
loc = matchedLoc;
}
;
try { 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;
}
}());
Copy link

ghost commented Oct 11, 2014

Really nice :)

@ChinaRedStar
Copy link

haha

@baxfx
Copy link

baxfx commented Jun 17, 2019

Does not work on Safari 12+

@GottZ
Copy link

GottZ commented Sep 2, 2019

@baxfx would be neat if you could show us a safari stacktrace then.

this is what a safari 12+ stacktrace looks like:

TypeError: undefined is not an object (evaluating 'this.tech_[a]')
  at techCall_(/js/video.min.js:17:27493)
  at volume(/js/video.min.js:17:29255)
  at ? (/js/w0bmscript.js:17:396)
  at ? (/js/video.min.js:14:6728)
  at forEach([native code])
  at ? (/js/video.min.js:14:6709)
  at d(/js/raven.min.js:2:6422)

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Error/Stack

this also confirms that this should work in safari 12+

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