1. Design source code
Copy codeThe Code is as follows:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> JavaScript retrieval 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 is my page">
<Script type = "text/javascript">
Function findPath ()
{
// Obtain the current URL
Var curNetAddr = too many Doc ument. location. href;
Alert ("Get current URL:" + curNetAddr );
// Obtain the directory after the host address
Var hostPath = specified parameter Doc ument. location. pathname;
Alert ("directory after obtaining the host address:" + hostPath );
// Returns the position of the first occurrence of a specified string value.
Var count = curNetAddr. indexOf (hostPath );
Alert ("return the position where a specified string value first appears in the string:" + count );
// Obtain the host address
Var hostAddr = curNetAddr. substring (0, count );
Alert ("Get host address:" + hostAddr );
// Obtain the project name "/"
Var projectName = hostPath. substring (0, hostPath. substr (1). indexOf ('/') + 1 );
Alert ("Get project name with"/":" + projectName );
// Obtain the project path
Var path = hostAddr + projectName;
Alert ("Get project path:" + path );
}
</Script>
</Head>
<Body>
<Div id = "body_div">
<Input type = "button" id = "btn" value = "obtain path" onclick = "findPath ()"/>
</Div>
</Body>
</Html>
2. design results
(1) During Initialization
(2) obtain the current website
(3) directory after obtaining the host address
(4) return the position where a specified string value first appears in the string.
(5) obtain the host address
(6) obtain the project name "/"
(7) obtain the project path