Get Current Path
“Get Current Path” (or “GCP”), will acquire the path of currently executing js file.
Install
<script src="./get-current-path.js"></script>
Usage
Use “GCP” as soon as possible, and avoid to use directly in any delays. (setTimeout, Promise, Event)
var currentPath = gcp()
Fallback
“GCP” uses currentScript
as default.
If the client browser does not support this feature (eg., IE),
it will fallback to a not reliable approach.
This fallback approach may not working when other async script presents.
You can try to pass the script filename to fix that.
var currentPath = gcp('your-script.js')
References
- How to get the file-path of the currently executing javascript code
- What is my script src URL
- document.currentScript is null
- Document.currentScript
Change Log
180903
* fallback with filename
180831
* initial release