Categories
JavaScript Answers

How to get the file path of the currently executing JavaScript code?

Spread the love

Sometimes, we want to get the file path of the currently executing JavaScript code.

In this article, we’ll look at how to get the file path of the currently executing JavaScript code.

How to get the file path of the currently executing JavaScript code?

To get the file path of the currently executing JavaScript code, we can use the document.currentScript property.

For instance, we write

const script = document.currentScript;
const fullUrl = script.src;

to get the currently running script with document.currentScript.

Then we get the path of the script with script.src.

Conclusion

To get the file path of the currently executing JavaScript code, we can use the document.currentScript property.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *