JS获取当前网址、主机地址项目根路径

来源:互联网 发布:深圳易信达软件 编辑:程序博客网 时间:2024/05/22 13:28
转载地址:http://www.shangxueba.com/jingyan/1901890.html


代码:

<!DOCTYPE html><html><head>    <title></title>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><script type="text/javascript">    //获取完整路径 为: http://localhost:6981/InspectionData/TEST.html        var fullPath=window.document.location.href;        //获取主机地址之后的路径 为: /InspectionData/TEST.html        var pathName=window.document.location.pathname;        //获取主机地址之后的路径长度 为: /InspectionData/TEST.html的长度为25        var pos=fullPath.indexOf(pathName);        //获取主机地址 为: http://localhost:6981        var localhostPath=fullPath.substring(0,pos);//获取项目名 为: /TEST.html        var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);                alert("fullPath为:\n"+fullPath+"\npathName为:\n"+pathName+"\npos为:\n"+pos+"\nlocalhostPath为:\n"+localhostPath+"\nprojectName为:\n"+projectName);</script></body></html>



在本机服务器上执行结果:




0 0
原创粉丝点击