When simply loading your script with a <script />
tag, you can retrieve the script URL like this:
var scriptURL = Array.prototype.pop.call ( // get the last...
document.getElementsByTagName ('script') // ...script tag on the page...
).src.split ('?') [0]; // ...take 'src' attribute...
// ...and strip the query string
This is a bit of a hack, but can be very useful sometimes, for a number of reasons (e.g., when there are other resource files script relies on and you don't want to hardcode the path). It works because at the time of execution the last <script />
tag existing on the page is your script.
I'm not sure if that is so when loading scripts with RequireJS. Is there a similar way in RequireJS to retrieve the script URL from inside the module definition?
You can require the module
module, which commonly used to pass the special config
settings:
define(['module'], function (module) {
console.log(module)
}
This will give you an object holding the id and the uri to the module
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