window.locationオブジェクトは、現在のページアドレス(URL)を取得し、ブラウザを新しいページにリダイレクトするために使用できます。
window.location オブジェクトは、windowの接頭辞を付けないで書くことができます。
いくつかの例:
window.location.href プロパティは、現在のページのURLを返します。
現在のページのhref(URL)を表示します:
document.getElementById("demo").innerHTML =
"Page location is " + window.location.href;
結果は次の通りです:
Try it Yourself »
window.location.hostname プロパティは、現在のページのインターネットホスト名を返します。
ホスト名を表示します:
document.getElementById("demo").innerHTML =
"Page hostname is " + window.location.hostname;
結果は次の通りです:
Try it Yourself »
window.location.pathname プロパティは、現在のページのパス名を返します。
現在のURLのパス名を表示します:
document.getElementById("demo").innerHTML =
"Page path is " + window.location.pathname;
結果は次の通りです:
Try it Yourself »
window.location.protocol プロパティは、ページのWebプロトコルを返します。
Webプロトコルを表示します:
document.getElementById("demo").innerHTML =
"Page protocol is " + window.location.protocol;
結果は次の通りです:
Try it Yourself »
window.location.assign() メソッドは、新しいドキュメントをロードします。
Load a new document:
<html>
<head>
<script>
function newDoc() {
window.location.assign("http://www.w3schools.com")
}
</script>
</head>
<body>
<input type="button" value="Load new document"
onclick="newDoc()">
</body>
</html>
Try it Yourself »