1. Design Source
Copy Code code as follows:
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>javascript Get Path </title>
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are my page" >
<script type= "Text/javascript" >
function Findpath ()
{
Get current URL
var curnetaddr = Window.document.location.href;
Alert ("Get current URL: + curnetaddr");
Get the directory after the host address
var hostpath = Window.document.location.pathname;
Alert ("Get the directory after the host address:" + Hostpath);
Returns the location of the first occurrence of a specified string value in a string
var count = Curnetaddr.indexof (Hostpath);
Alert ("Returns the location of the first occurrence of a specified string value in the string:" + count);
Get host Address
var hostaddr = curnetaddr.substring (0,count);
Alert ("Get host Address: + HOSTADDR");
Get the name of the project with "/"
var projectname = hostpath.substring (0,hostpath.substr (1). IndexOf ('/') +1);
Alert ("Get the project name with/": + ProjectName);
Get Project Path
var path = hostaddr + projectname;
Alert ("Get Project path:" + path);
}
</script>
<body>
<div id= "Body_div" >
<input type= "button" id= "btn" value= "Get Path" onclick= "Findpath ()"/>
</div>
</body>
2. Design result
(1) When initializing
(2) Get the current URL
(3) Get the directory after the host address
(4) Returns the location of the first occurrence of a specified string value in a string
(5) Get the host address
(6) Get the project name with "/"
(7) Get the project path