5

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 is given for height or width,then it opens in a new tab.

Is there any property through which I can determine window opened was a pop up or a new tab?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
malcolmX
  • 429
  • 4
  • 15
  • Possible duplicate of http://stackoverflow.com/questions/10240398/check-whether-a-window-is-popup-or-not – immutabl Feb 19 '13 at 09:52

3 Answers3

4

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

Robert Fricke
  • 3,637
  • 21
  • 34
  • 1
    Its probably best you don't use w3schools as a reference. – immutabl Feb 19 '13 at 09:58
  • Why is that problably best? I learned a lot from there in my early days. – Robert Fricke Feb 19 '13 at 10:28
  • Hello Robert Fricke, Thanks for the quick reply.I have made a webpage which is opened by a 3rd party button.I don't have any control on that button.Initially a pop up was getting opened if I clicked on that 3rd party button.The attributes has been set by them.There is another 3rd party personnel who created a different button which opens same webpage.Only difference is ,if I click on that button it opens as a new tab.So I was thinking is there any way of distinguishing window open mechanism i.e whether it was open as a new tab or as a pop up on top of the current window.? – malcolmX Feb 19 '13 at 10:30
  • @5arx i checked that post but that functionlity is not working :( – malcolmX Feb 19 '13 at 10:32
  • @5arx window.location.href == window.opener.location.href is returning me false all the time....Is there any other alternative? – malcolmX Feb 19 '13 at 10:46
  • @Robert Fricke - W3Schools is not generally regarded as a valid authority by many within the SO community: http://meta.stackoverflow.com/search?q=w3schools (see also http://w3fools.com/), there are better-maintained sites with better, more accurate information. e.g. https://developer.mozilla.org – immutabl Feb 19 '13 at 11:06
  • 1
    I am also puzzled by this now.. seems like `window.parent.parent.parent`... and so on keeps returning a window in eternity. And I also noticed `window.open()` opens a real window in a new tab sometimes, and a popup child window other times. I am guessing it is linked to how the event is triggered, ie: if a key or mouse event triggered window open, a window is opened. If just plain code triggered `window.open()` then a popup is opened. When your site is opened in it's own window, is it a popup one where you can not change the address? Or a normal window? – Robert Fricke Feb 19 '13 at 12:17
  • It's like this Two different third party created a button which opens up my content.One opens in a tab and other opens in a popup.I try to maximize the pop up window using window.resizeTo functionality.I added this piece of code in my program.So in this way,whatever height the third party added,it gets overridden with my window.resizeTo function.So pop up window will open in a full screen. The other button which was opening as a tab should open as a tab alone.But it is opening as a new pop up with screen.height and screen.width. Hope you get the picture by now. – malcolmX Feb 19 '13 at 12:33
  • Is there any way of categorising them as pop up or as new tab? – malcolmX Feb 21 '13 at 06:16
  • Does my edit help you at all? I understand IE is quite important though – Robert Fricke Feb 21 '13 at 19:34
  • Thank you Robert.You saved me.For IE it's not working though.But anyway we don't have too many customers for IE.Anyway thanks and cheers. :) – malcolmX Feb 25 '13 at 10:30
  • In Safari, `window.menubar.visible` is false for windows and tabs, so not a particularly good method of detection. `window.menubar` is not part of any W3C specification, support is limited to "DOM 0", i.e. features that existed in popular browsers before W3C DOM standards (more than 15 years ago) and were not formally standardised. – RobG Nov 20 '13 at 12:13
0

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

crimsoniris
  • 84
  • 1
  • 3
  • 11
  • Hello crimsoniris, As I have mentioned in my post,I am able to open a new window(be it a pop up or a new tab). My straight forward question is:- Is there any inbuilt function with which I can understand if thw window opened itself was a new tab or a pop up? P:S-By pop up it means,A window opening on top of current window. Thanks for the quick reply by the way – malcolmX Feb 19 '13 at 10:20
  • @Crimsoniris - I repeat the usual caveat about www.w3fools.com ;-) – immutabl Feb 19 '13 at 11:19
0

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>
Godwin
  • 400
  • 3
  • 15