Check whether a window is Popup or not?

前端 未结 10 1446
夕颜
夕颜 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条回答
  •  旧时难觅i
    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();
    }
    

提交回复
热议问题