Search code examples
javascripthtmlwindow.open

How to check if a window was instantiated from a window.open()


Is it possible to check if a window was opened from a window.open() with the new opened window itself. I only want the website I am opening to be display a specific context when opened from a window.open(). It is cross-origin. For example: if the URL is accessed and not opened from a window.open(), I want it to redirect elsewhere. I know this is possible from the parent window, but not sure if it can be done from the child window

Thanks!


Solution

  • You can use window.opener. This will return a reference to the window that opened the current window or null if it is not opened by open().

    if (window.opener) {
        // Opened from another window
    } else {
        // Not opened from another window
    }