JavaScript to get the path design source


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

(2) get the current url

 

(3) get the directory after the host address

 

(4) returns 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