How can I find out the URL of the JS file that was called?
Let's say the JS file (which is part of a library) and the HTML file (which is the application) are on a different server. To find out the URL of the HTML file is easy. But how can I find out the server name and path of the JS files' server within the JS file itself? (Edit: There is a loader which invokes other JS files of thsi library in the same directory structure)
Preferably a JQuery or pure JS solution.
Edit:
After I learned from your answers and comments I decided it would be cleaner to actually move the loader code to the first server, where also the html lives, which avoids the problem.
Answer: Use the window. location. href Property location. href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc.
Definition and Usage. The src attribute specifies the URL of an external script file. If you want to run the same JavaScript on several pages in a web site, you should create an external JavaScript file, instead of writing the same script over and over again.
Window Location Hrefhref property returns the URL of the current page.
In JQuery you could do:
$('script').each(function(i, e) {
alert($(e).attr('src'));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With