How do identify whether the window opened is a pop up or a tab?

前端 未结 3 835
陌清茗
陌清茗 2020-12-19 16:24

I have been facing a problem.I am able to open a window using window.open method.If I specify the height and width of the window,it opens as a pop up window.If no parameters

相关标签:
3条回答
  • 2020-12-19 17:02

    You can detect that using onblur, by checking whether the focus is missed or not

    <html>
    <head>
    <script>
    function newTab() {
      document.getElementById("demo").innerHTML = "New tab opened!<br><br>refesh this page to recheck ";
    }
    window.onblur = newTab;
    </script>
    </head>
    <body>
    <div id="demo">
    Open a new tab and then check this page
    </div>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-19 17:13

    I'm not quite sure what you mean in your question but from what I understand, you might want to use the HTML target attribute:

    _blank  Opens the linked document in a new window or tab
    _self   Opens the linked document in the same frame as it was clicked (this is default)
    _parent     Opens the linked document in the parent frame
    _top    Opens the linked document in the full body of the window
    

    framename Opens the linked document in a named frame

    Source: http://www.w3schools.com/tags/att_a_target.asp

    0 讨论(0)
  • 2020-12-19 17:24

    Edit: I have been looking into this a little further.

    Seems like there is no different "type" on these windows, simply different options. A way I found to check if it was a tab or window is to check window.menubar.visible. For the tab, which is a full and normal window it is true, and for the pop-up the menu is hidden and therefore false. Same applies to window.toolbar.visible.

    Works in FF and Chrome at least. Unfortunately not in IE. (Testing done in IE8, which is the version I have installed. For testing of course..)

    Example:

    if(window.menubar.visible) {
        //Tab
    } else {
        //"Child" Window
    }
    

    Found this thread: Internet Explorer 8 JS Error: 'window.toolbar.visible' is null or not an object


    If you specify width and height, it means that you also have to specify the name parameter. This can be used in the same way target in an a tag is used, and defaults to _blank.

    If you do not specify width and height I assume you also don't specify name and therefore it is opened with name=_blank, which means a new Tab.

    If you specify width and height, are you setting a custom name? Doing so results in a child window. If you specify a name, or empty string as name, I suggest you try name:_blank if you want it to be a new tab.

    If the window was opened with a name, you can always the window.parent from the child window. If you open with _blank I am not sure if you can get the window.parent

    w3schools Window Open

    0 讨论(0)
提交回复
热议问题