How to get the file-path of the currently executing javascript code

后端 未结 10 862
南笙
南笙 2020-11-29 19:18

I\'m trying to do something like a C #include \"filename.c\", or PHP include(dirname(__FILE__).\"filename.php\") but in javascript. I know I can do

10条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 19:24

    All browsers except Internet Explorer (any version) have document.currentScript, which always works always (no matter how the file was included (async, bookmarklet etc)).

    If you want to know the full URL of the JS file you're in right now:

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

    Tadaa.

提交回复
热议问题