Get Caller JavaScript File Location

If you need to get the Path to the current Javascript file being executed, you can use this small function:
 const location = (function () {
                if (document.currentScript) {
                    let link = document.currentScript.src;
                    let lastIndex = link.lastIndexOf('/');
                    link = link.substring(0, lastIndex);
                    return link;
                }
 })();

In our code base, we use it to dynamically load Component dependencies.

This entry was posted in HTML Elements, Web Components and tagged , , . Bookmark the permalink.

Leave a Reply