JavaScript to get the path design source
- 2020-03-30 03:04:17
- OfStack
1, design the source code
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>JavaScript Access 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);
//Gets the directory after the host address
var hostPath = window.document.location.pathname;
alert(" Directory after getting host address: " + hostPath);
//Returns the first occurrence of a specified string value in a string
var count = curNetAddr.indexOf(hostPath);
alert(" Returns the first occurrence of a specified string value in a string: " + count);
//Get host address
var hostAddr = curNetAddr.substring(0,count);
alert(" Get host address: " + hostAddr);
//Gets the project name with "/"
var projectName = hostPath.substring(0,hostPath.substr(1).indexOf('/')+1);
alert(" Access to" / Project name: " + projectName);
//Get project path
var path = hostAddr + projectName;
alert(" Get project path: " + path);
}
</script>
</head>
<body>
<div id="body_div">
<input type="button" id="btn" value=" Access path " onclick="findPath()"/>
</div>
</body>
</html>
2. Design results
(1) initialization
< img Alt = "" border = 0 border = 1 SRC =" / / files.jb51.net/file_images/article/201405/201405220937172.png "width = 81 height = 42 >
(2) get the current url
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201405/201405220937173.png ">
(3) get the directory after the host address
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201405/201405220937174.png ">
(4) returns the first occurrence of a specified string value in a string
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201405/201405220937175.png ">
(5) get the host address
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201405/201405220937176.png ">
(6) get the project name with "/"
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201405/201405220937187.png ">
(7) get the project path
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201405/201405220937188.png ">