Web制作の必須知識!windowオブジェクトの主要プロパティ・メソッドまとめ

window.locationの主なobject

URLが https://example.com:8080/path/page.html?id=123&name=tom#section1 の場合:

  • window.location.href: URL全体 (https://...)
  • window.location.protocol: プロトコル (https:)
  • window.location.host: ドメインとポート番号 (example.com:8080)
  • window.location.pathname: ドメイン以降のパス (/path/page.html)
  • window.location.search: クエリパラメータ (?id=123&name=tom)
  • window.location.hash: アンカー/フラグメント (#section1)

よく使われるプロパティー

window. は通常省略して使われる

  • window.document: 表示されているページのHTML(DOM)へのアクセス。
  • window.console: コンソールへの出力(console.log)。
  • window.location: URLの取得やページ遷移(location.href)。
  • window.localStorage / sessionStorage: ブラウザへのデータ保存。
  • window.alert() / prompt() / confirm(): ダイアログボックスの表示。
  • window.setTimeout() / setInterval(): タイマー処理。
  • window.addEventListener(): イベント(ロード、スクロールなど)の監視。 

おまけ

window と document の違い

  • windowブラウザのウィンドウ全体を指す。
  • document: ウィンドウの中に表示されているHTMLドキュメント(ページ)を指す。
タイトルとURLをコピーしました