Check whether a window is Popup or not?

前端 未结 10 1401
夕颜
夕颜 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:29

    window.locationbar.visible not necessarily only for a popup, but can help detecting if the user can change the location manually via location bar...

    0 讨论(0)
  • 2020-12-08 20:29

    This is not a sure fire method, but typically a popup using window.open() does not direct to a new URL, so both windows will have the same href. Therefore, in many cases:

    window.location.href == window.opener.location.href
    

    will be true for a popup. So you could do:

    var isPopup = (window.location.href == window.opener.location.href);
    

    Like I said, this works in my limited tests, so there may be some feedback that shows where this won't be reliable.

    0 讨论(0)
  • 2020-12-08 20:29

    The best way, is simply to check the window width, then try to resize the window width in a pixel or two, and then check if the current width equals or not to the one before the resize

    in other words, if you succeeded to resize the window, you're probably inside a popup.

    good luck.

    0 讨论(0)
  • 2020-12-08 20:29

    I was implementing 'close' button for popup window screen and 'cancel' button for '_self' window in "Typescript/Nodejs" and believe it will work for JavaScript as well. So I needed to determine if screen is opened in a popup

    if(window.opener || window.history.length === 1) 
         isPopupWindow = true;
    else 
       isPopupWindow = false;
    

    "window.opener" works for almost all the browsers but having issue with IE so to handle IE issue I used "window.history.length".

    0 讨论(0)
  • 2020-12-08 20:36

    Why not just check if the opener is NOT undefined? a popup has an opener while a regular page hasn't.

    if (window.opener != null) 
    
    0 讨论(0)
  • 2020-12-08 20:38

    I've found that some browsers will set window.opener to window in certain cases. This is the most reliable popup check that I am using right now.

    if (window.opener && window.opener !== window) {
      // you are in a popup
    }
    
    0 讨论(0)
提交回复
热议问题