現在表示中のURLを取得 |
現在表示中のURLを取得 |
| プロパティ名 | 取得する結果 | (例) |
| location.href | 完全なURL | https://nekofes.github.io/android/index.html?lang=0#section |
| location.protocol | プロトコル | https: |
| location.host | プロトコル | https: |
| location.host | ホスト名とポート番号 | localhost:8080 |
| location.hostname | ホスト名 | nekofes.github.io |
| location.port | ポート番号 | 8080 |
| location.pathname | パス名 | /android/index.html |
| location.search | クエリパラメータ(?から始まる部分) | ?lang=0 |
| location.hash | ハッシュ(#から始まる部分) | #section |
function getPathName()
{
return window.location.pathname;
}
function fileId()
{
const pathname = getPathName();
return pathname.split('/').pop();
}
function directoryPath()
{
const pathname = getPathName();
const lastSlashIndex = pathname.lastIndexOf('/');
return pathname.substring(0, lastSlashIndex + 1);
}
console.log(getPathName());// /js/js94.html
console.log(fileId());// js94.html
console.log(directoryPath());// /js/
| 実行した関数 | 結果 |
| getPathName() | /index.html |
| fileId() | index.html |
| directoryPath() | / |
| ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |