1、設計源碼
復制代碼 代碼如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>JavaScript獲取路徑</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()
{
//獲取當前網(wǎng)址
var curNetAddr = window.document.location.href;
alert("獲取當前網(wǎng)址:" + curNetAddr);
//獲取主機地址之后的目錄
var hostPath = window.document.location.pathname;
alert("獲取主機地址之后的目錄:" + hostPath);
//返回某個指定的字符串值在字符串中首次出現(xiàn)的位置
var count = curNetAddr.indexOf(hostPath);
alert("返回某個指定的字符串值在字符串中首次出現(xiàn)的位置:" + count);
//獲取主機地址
var hostAddr = curNetAddr.substring(0,count);
alert("獲取主機地址:" + hostAddr);
//獲取帶“/”的項目名
var projectName = hostPath.substring(0,hostPath.substr(1).indexOf('http://www.survivalescaperooms.com/')+1);
alert("獲取帶“/”的項目名:" + projectName);
//獲取項目路徑
var path = hostAddr + projectName;
alert("獲取項目路徑:" + path);
}
</script>
</head>
<body>
<div>
<input type="button" value="獲取路徑"/>
</div>
</body>
</html>
(1)初始化時
(2)獲取當前網(wǎng)址
(3)獲取主機地址之后的目錄
(4)返回某個指定的字符串值在字符串中首次出現(xiàn)的位置
(5)獲取主機地址
(6)獲取帶“/”的項目名
(7)獲取項目路徑
新聞熱點
疑難解答
圖片精選