Skip to content

Instantly share code, notes, and snippets.

@gladchinda
Last active May 12, 2022 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gladchinda/9de985867a1f08e13ae95350e0c1cc62 to your computer and use it in GitHub Desktop.
Save gladchinda/9de985867a1f08e13ae95350e0c1cc62 to your computer and use it in GitHub Desktop.
Get useful browsing context information about a window.
function determineBrowsingContext(win = window) {
/**
* ---------------------------------------------------------------------------
* The source browsing context of the specified window object or subframe.
* ---------------------------------------------------------------------------
* It can be a reference to the parent browsing context (for frames and other
* embedded windows), or the opener browsing context (for windows opened from
* another window).
*
* For windows opened from another window, it is possible not to have access
* to the opener browsing context, depending on how the window was opened
* (e.g link opened with `rel=noopener`). In such cases, the source browsing
* context is `null`.
*/
const $source = win.opener || (win.parent !== win && win.parent) || null;
return Object.create(null, {
$source: { value: $source },
/**
* -------------------------------------------------------------------------
* Boolean value that indicates whether the specified window is embedded.
* -------------------------------------------------------------------------
* A window object is said to be "embedded" if it's a child browsing context
* contained by an element within its source browsing context. This is `true`
* for iframes, frames within a `<frameset>` element, etc.
*/
embedded: { value: win.parent === $source },
/**
* -------------------------------------------------------------------------
* Boolean value that indicates whether the specified window is standalone.
* -------------------------------------------------------------------------
* A window object is said to be "standalone" if no reference to a source
* browsing context can be found for it, and if it is the topmost window in
* its hierarchy of windows.
*/
standalone: { value: !$source && win.top === win }
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment