获取js文件名和js路径和网站根目录的工具类

mpath获取文件和目录工具类

class _mpath{
    /*
    getfilename()
    getfilepath()
    getrootpath()
    */
/*
获取文件名
*/
getfilename(){
return window.location.pathname.split('/')[a.length - 1];
}
/*
获取不含文件名的当前目录
*/
getfilepath(){
return window.location.pathname.substring(0,window.location.pathname.lastIndexOf('/'));
}
/*
 获取网站根路径
*/
getrootpath(){
        var strFullPath=window.document.location.href;
        var strPath=window.document.location.pathname;
        var pos=strFullPath.indexOf(strPath);
        var prePath=strFullPath.substring(0,pos);
        var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);
        return(prePath+postPath);
       }
}
var mpath=new _mpath();

把以上代码保存为js文件即可使用。
我们这里假设保存为class.js文件。
上面是一个工具类,下面是调用方法:
假设下面的文件保存为index.html则:

<html>
<head>
<title>Yuanwowjs工具类演示</title>
<script src="class"></script>
<script>
//控制台查看...
mpath.getfilename();//=>"index.html"
mpath.getfilepath();//=>"/test/"
mpath.getrootpath();//=>"www.xxx.com/"
</script>
<body>
yuanwow演示js使用文档,请查看浏览器控制台查看内容.<br>
如果你是手机写网页可以在要获取的值外面套一个alert😊
</body>
</html>

就这样吧(其实我是不知道怎么结尾比较好)
点赞和关注是对我最好的支持!我以后会写更多实用的文章的。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值