Check whether a window is Popup or not?

前端 未结 10 1416
夕颜
夕颜 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

    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.

提交回复
热议问题