1。ソースコードを設計します
コードコピーは次のとおりです。
<%@ページ言語= "java" import = "java.util。*" pageencoding = "utf-8"%>
<!doctype html public " - // w3c // dtd html 4.01 transitional // en">
<html>
<head>
<Title> JavaScript Get Path </title>
<メタhttp-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<メタhttp-equiv = "expires" content = "0">
<meta http-equiv = "keywords" content = "keyword2、keyword3">
<メタhttp-equiv = "description" content = "これは私のページ">
<script type = "text/javascript">
関数findpath()
{
//現在のURLを取得します
var curnetaddr = window.document.location.href;
alert( "現在のURLを取得:" + curnetaddr);
//ホストアドレスの後にディレクトリを取得します
var hostpath = window.document.location.pathname;
alert( "ホストアドレス後にディレクトリを取得します:" + hostpath);
//文字列内の指定された文字列値の最初の発生を返します
var count = curnetaddr.indexof(hostpath);
alert( "指定された文字列値が最初に文字列に表示される場所を返します:" + count);
//ホストアドレスを取得します
var hostaddr = curnetaddr.substring(0、count);
alert( "ホストアドレスを取得:" + hostaddr);
//「/」でプロジェクト名を取得します
var projectname = hostpath.substring(0、hostpath.substr(1).indexof( '/')+1);
alert( "/"でプロジェクト名を取得: " + projectName);
//プロジェクトパスを取得します
var path = hostaddr + projectname;
アラート( "プロジェクトパスを取得:" +パス);
}
</script>
</head>
<body>
<div id = "body_div">
<入力型= "button" id = "btn" value = "get path" onclick = "findpath()"/>
</div>
</body>
</html>
2。デザインの結果
(1)初期化
(2)現在のURLを取得します
(3)ホストアドレスの後にディレクトリを取得します
(4)指定された文字列値が最初に文字列に表示される場所を返します
(5)ホストアドレスを取得します
(6)「/」でプロジェクト名を取得します
(7)プロジェクトパスを取得します