3

In html, if I load an external javascript file like this

<script src="public/js/mycode.js"></script>

then in mycode.js file, is there some javascript function code I can call and it will return the string "mycode.js" or even "public/js/mycode.js".

I don't want to hardcode any information about the script tag such as the filename.

Does anyone know a way?

Thanks

omega
  • 40,311
  • 81
  • 251
  • 474

2 Answers2

2

document.currentScript will return the currently executing script although I doubt older browsers support this.

Feature        Chrome   Firefox (Gecko)   Internet Explorer Opera   Safari
Basic support  29       4.0 (2.0)         Not supported     16      8

Unfortunately as usual IE is late to the game it looks like.

From: https://developer.mozilla.org/en-US/docs/Web/API/Document/currentScript

AtheistP3ace
  • 9,611
  • 12
  • 43
  • 43
  • I think as long as its supported in IE11+ and relatively newer versions of firefox/chrome. – omega Dec 03 '15 at 14:50
  • @omega it is entirely dependent on your implementation, but just in case you need to know the current support go here: https://developer.mozilla.org/en-US/docs/Web/API/Document/currentScript – AGE Dec 03 '15 at 14:51
0

Use onload attribute at script tag ?

<script onload="console.log(this.src)" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
guest271314
  • 1
  • 15
  • 104
  • 177