1. Design source code
The code copy 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 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 is my page">
<script type="text/javascript">
function findPath()
{
//Get the current URL
var curNetAddr = window.document.location.href;
alert("get the 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 first occurrence of a specified string value in the string
var count = curNetAddr.indexOf(hostPath);
alert("Returns the location where a specified string value first appears in the string: " + count);
//Get the host address
var hostAddr = curNetAddr.substring(0,count);
alert("get host address:" + hostAddr);
//Get the project name with "/"
var projectName = hostPath.substring(0,hostPath.substr(1).indexOf('/')+1);
alert("get the project name with "/": " + projectName);
//Get 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="get path" onclick="findPath()"/>
</div>
</body>
</html>
2. Design results
(1) Initialization
(2) Get the current URL
(3) Obtain the directory after the host address
(4) Returns the location where a specified string value first appears in the string
(5) Obtain the host address
(6) Get the project name with "/"
(7) Get the project path