简爱博客
获取 JS 文件自身地址
2014-12-1 简爱
有特殊用处的 函数

返回的为当前 js 的网址信息:

地址 url

路径 path

名称 name

当然没考虑 GET 部分,这不是本文 讨论的内容

这是感觉 CSS 网址有相对于 CSS 文件 的路径写法如 url(../img/icon.png)js 就无法使用 所以就用到它了,偶尔用用还是不要依赖这玩意儿的好[阴险/]

console.log(baseUrl());
function baseUrl(){
var script = document.getElementsByTagName('script'),
len = script.length,
url = script[len-1].src,
path = url.replace(/^(.*)\/.*/, '$1\/'),
name = url.replace(/^(.*)\//, '');
return {url: url, path: path, name: name};
}