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

后端 未结 10 836
南笙
南笙 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:37

    I've more recently found a much cleaner approach to this, which can be executed at any time, rather than being forced to do it synchronously when the script loads.

    Use stackinfo to get a stacktrace at a current location, and grab the info.file name off the top of the stack.

    info = stackinfo()
    console.log('This is the url of the script '+info[0].file)
    

提交回复
热议问题