一、Google,FF浏览器获取当前js文件的路径
document.currentScript.src
//Google,FF 下获取当前js文件的路径
// IE10+、Safari和Opera9不支持
console.info(document.currentScript.src);
二、比较兼容的获取方式(推荐)
//页面解析到当前为止所有的script标签
var js = document.scripts;
//js[js.length - 1] 就是当前的js文件的路径
js = js[js.length - 1].src.substring(0, js[js.length - 1].src.lastIndexOf("/") + 1);
//输出当前js文件所在的目录
//console.info(js);
如路径为 http://localhost:24395/ABC/XX/a.js
结果会得到 http://localhost:24395/ABC/XX/
更多: