Check whether a window is Popup or not?

前端 未结 10 1402
夕颜
夕颜 2020-12-08 20:23

This question is not the duplicate of if window is popup , But a similar one.

I am developing a extension which injects scripts to all web pages. I need to detect w

相关标签:
10条回答
  • 2020-12-08 20:46

    The following statement will be true if window is a popup window or subframe:

     window.parent != window
    
    0 讨论(0)
  • 2020-12-08 20:47

    The following worked for me when testing in Chrome, Firefox, Safari, and IE8. It worked for a window created useing window.open() or target=_blank.

    if (window.opener) {
        alert('inside a pop-up window or target=_blank window');
    } else if (window.top !== window.self) {
        alert('inside an iframe');
    } else {
        alert('this is a top level window');
    }
    
    0 讨论(0)
  • 2020-12-08 20:53

    For me, i was implementing a Logout screen that could be shown in a window popup via window.open(..) or from a link within a single page.

    I needed to determine:

    1) if my Logout screen is within a popup, then:

    event.preventDefault();
    window.close();
    

    2) otherwise use the browser back function..

    window.history.back();
    

    My solution: If the page is within a popup, in my case it has a window page history of 1:

    event.preventDefault();
    if (window.history.length === 1) {
        window.close();
    } else {
        window.history.back();
    }
    
    0 讨论(0)
  • 2020-12-08 20:56

    I use this code to determine a popup window

    (window.opener && window.opener !== window && !window.menubar.visible)
    
    0 讨论(0)
提交回复
热议问题