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