Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
__FILE__ in JavaScript
* This gist provides a __FILE__ variable, which holds the path to the file
* from which the currently running source is being executed.
* Usage example: alert(__FILE__);
* Thanks to on which this gist
* is based on.
* Tested in Mozilla Firefox 9, Mozilla Firefox 16, Opera 12, Chromium 18
this.__defineGetter__("__FILE__", function() {
var stack=((new Error).stack).split("\n");
if(stack[0]=="Error") { // Chromium
var m;
return m[1];
else { // Firefox, Opera
return stack[1].split("@")[1].split(":").slice(0,-1).join(":");
Copy link

plepe commented Oct 15, 2012

About this GIST:

Copy link

BR0kEN- commented Jan 25, 2014

Simplified and shortened code:

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