「JavaScript」location.pathnameでURLのパス名を取得するサンプル
書式
string = object.pathname;
URL.pathname
URL インターフェイスの pathname プロパティは、最初の '/’ とその後に続く URL のパス(またはパスがない場合は
空の文字列)を含む USVString です。
URLパス名とは、URL中のディレクトリとファイル名部分です。
使用例
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title> URLのパス名を取得するサンプル</title>
<script>
function funA(){
// 現在のページのURLパス名を取得
var res =location.pathname;
// URLのパス名を表示する
alert(res);
}
</script>
</head>
<body>
<input type="button" value="検証">
</body>
</html>
結果
「検証」ボタンを押すと、URLのパス名を取得します。