「windowオブジェクトが、イマイチよくわからない・・・」
「JavaScriptのwindowオブジェクト一覧を知りたい」
このような場合に、この記事の内容が参考となります。
この記事では、JavaScriptのwindowオブジェクトについて解説しています。
また、windowからグローバル変数を除いた一覧も載せています。
本記事の内容
- windowオブジェクトとは?
- windowオブジェクト一覧
それでは、上記に沿って解説していきます。
windowオブジェクトとは?
windowオブジェクトとは、JavaScriptが関わるすべての源です。
DOMのdocumentオブジェクトは、windowオブジェクトのdocumentプロパティのことです。
つまり、windowオブジェクトの下にdocumentオブジェクトが存在しています。
また、グローバル変数もwindowオブジェクトのプロパティとしてアクセスできます。
例えば、次のようなグローバル変数があったとします。
var global_param = "GLOBAL"; console.log(global_param); console.log(window.global_param); console.log(global_param === window.global_param);
上記コードの実行結果は、以下。
Visual Studio Codeで開発中においても、このことを実感できます。
「window.」と入力した時点で候補として出てくるのです。
さらには、関数もwindowオブジェクトのプロパティとしてアクセスできます。
例えば、みんながよく知っているこれです。
window.alert("alert!!");
これも、使ったことはあるでしょう。
window.open('https://example.com/');
実は、console.logもwindowファミリーなんです。
window.console.log("console.logもそうなんです");
普段は、windowを省略して利用しているだけと言えます。
「windowオブジェクトとは、JavaScriptが関わるすべての源です」
この言葉は、決して誇張したモノではありません。
以上、windowオブジェクトについて説明しました。
次は、windowオブジェクトの中身を確認します。
windowオブジェクト一覧
確認ページは、以下のコード以外は実行されていないページです。
つまり、プラグインなどで余計なグローバル変数が宣言されていないページになります。
ブラウザ固有のwindowオブジェクト一覧のみということです。
そのwindowオブジェクトの中身(プロパティ)は、次のコードで確認できます。
for (const key of Object.keys(window)) { console.log(key); }
上記を実行した結果は、以下。
全部で、212個のプロパティが確認できました。
なお、これはChrome(96.0.4664.45)ブラウザで確認したときの結果となります。
ブラウザによって、利用できるプロパティは変わってきます。
また、拡張機能などもすべて停止した状態です。
windowオブジェクト(個別のグローバル変数を除いた)一覧
※利用しやすいようにカンマ区切りとしています
"window","self","document","name","location","customElements","history","locationbar","menubar","personalbar","scrollbars","statusbar","toolbar","status","closed","frames","length","top","opener","parent","frameElement","navigator","origin","external","screen","innerWidth","innerHeight","scrollX","pageXOffset","scrollY","pageYOffset","visualViewport","screenX","screenY","outerWidth","outerHeight","devicePixelRatio","clientInformation","screenLeft","screenTop","defaultStatus","defaultstatus","styleMedia","onsearch","isSecureContext","performance","onappinstalled","onbeforeinstallprompt","crypto","indexedDB","webkitStorageInfo","sessionStorage","localStorage","onbeforexrselect","onabort","onblur","oncancel","oncanplay","oncanplaythrough","onchange","onclick","onclose","oncontextmenu","oncuechange","ondblclick","ondrag","ondragend","ondragenter","ondragleave","ondragover","ondragstart","ondrop","ondurationchange","onemptied","onended","onerror","onfocus","onformdata","oninput","oninvalid","onkeydown","onkeypress","onkeyup","onload","onloadeddata","onloadedmetadata","onloadstart","onmousedown","onmouseenter","onmouseleave","onmousemove","onmouseout","onmouseover","onmouseup","onmousewheel","onpause","onplay","onplaying","onprogress","onratechange","onreset","onresize","onscroll","onseeked","onseeking","onselect","onstalled","onsubmit","onsuspend","ontimeupdate","ontoggle","onvolumechange","onwaiting","onwebkitanimationend","onwebkitanimationiteration","onwebkitanimationstart","onwebkittransitionend","onwheel","onauxclick","ongotpointercapture","onlostpointercapture","onpointerdown","onpointermove","onpointerup","onpointercancel","onpointerover","onpointerout","onpointerenter","onpointerleave","onselectstart","onselectionchange","onanimationend","onanimationiteration","onanimationstart","ontransitionrun","ontransitionstart","ontransitionend","ontransitioncancel","onafterprint","onbeforeprint","onbeforeunload","onhashchange","onlanguagechange","onmessage","onmessageerror","onoffline","ononline","onpagehide","onpageshow","onpopstate","onrejectionhandled","onstorage","onunhandledrejection","onunload","alert","atob","blur","btoa","cancelAnimationFrame","cancelIdleCallback","captureEvents","clearInterval","clearTimeout","close","confirm","createImageBitmap","fetch","find","focus","getComputedStyle","getSelection","matchMedia","moveBy","moveTo","open","postMessage","print","prompt","queueMicrotask","releaseEvents","reportError","requestAnimationFrame","requestIdleCallback","resizeBy","resizeTo","scroll","scrollBy","scrollTo","setInterval","setTimeout","stop","webkitCancelAnimationFrame","webkitRequestAnimationFrame","chrome","caches","cookieStore","ondevicemotion","ondeviceorientation","ondeviceorientationabsolute","showDirectoryPicker","showOpenFilePicker","showSaveFilePicker","originAgentCluster","trustedTypes","speechSynthesis","onpointerrawupdate","crossOriginIsolated","scheduler","openDatabase","webkitRequestFileSystem","webkitResolveLocalFileSystemURL"
以上、windowオブジェクト一覧についての説明でした。