Google Chrome "window.open" workaround?
Asked Answered
S

9

65

I have been working on a web app and for part of it I need to open a new window. I have this working on all browsers, my sticking point is with Google Chrome.

Chrome seems to ignore the window features which is causing me issues, the thing I'm struggling with is I need the address bar to be editable within the new window. FF, IE, Safari and Opera do this fine, Chrome does not.

My Code:

function popitup(url) {
  newwindow=window.open(url, 'name', 'toolbar=1,scrollbars=1,location=1,statusbar=0,menubar=1,resizable=1,width=800,height=600');
  if (window.focus) {
    newwindow.focus()
  }
  return false;
}
Salve answered 3/4, 2010 at 19:7 Comment(6)
Wow - that's pretty weird. Some kind of misguided 'security' feature, I imagine.Insurer
yeah its an odd one, took some working to get this far! lolSalve
This probably isn't what you want to hear, but don't open things in new windows/tabs. If the user wanted a new window, they can ask their browser to do it.Dibbrun
thanks jamesdlin, its actually a web dev tool for testing browser sizes so its kinda essential :)Salve
check this question #4994563Skit
The behavior also depends on where your popitup funtion is called from, which probably causes the confusion. See my answer below.Sidestep
S
38

The other answers are outdated. The behavior of Chrome for window.open depends on where it is called from. See also this topic.

When window.open is called from a handler that was triggered though a user action (e.g. onclick event), it will behave similar as <a target="_blank">, which by default opens in a new tab. However if window.open is called elsewhere, Chrome ignores other arguments and always opens a new window with a non-editable address bar.

This looks like some kind of security measure, although the rationale behind it is not completely clear.

Sidestep answered 13/5, 2013 at 23:18 Comment(1)
Do you know if there's some official documentation about this?Une
L
26

This worked for me:

newwindow = window.open(url, "_blank", "resizable=yes, scrollbars=yes, titlebar=yes, width=800, height=900, top=10, left=10");
Laura answered 5/8, 2011 at 17:38 Comment(2)
It depends on where it is called from, see my answer below.Sidestep
isn't working for me with a named window (it's opening a new window rather than an existing one), regardless of where it's being called from @jeroenSorilda
W
9

The location=1 part should enable an editable location bar.

As a side note, you can drop the language="javascript" attribute from your script as it is now deprecated.

update:

Setting the statusbar=1 to the correct parameter status=1 works for me

Wingfooted answered 3/4, 2010 at 19:52 Comment(2)
thanks for the reply scunliffe :) I just tried with those settings and the location bar displays the URL but still cannot be edited :(Salve
Thanks - I was missing the status=1 part of the params, and that had prevented chrome from treating it as a full-featured window.open(). When I put it in, it started opening windows in tabs.Paley
C
5

I believe currently there is no javascript way to force chrome to open as a new window in tab mode. A ticket has been submitted as in here Pop-ups to show as tab by default. But the user can click the chrome icon on the top left corner and select "Show as tab", the address bar then becomes editable.

A similar question asked in javascript open in a new window not tab.

Commendation answered 23/4, 2010 at 20:57 Comment(4)
If you don't specify and window properties when you open a window, it should put it in a tab. Conversely, if you want a window to open instead of a tab, specify at least one window property.Arjun
The OP's problem is not about opening as a new window or tab. In chrome, OP is opening a new window, but the location bar is grayed out. The address bar is only editable when the new window is "show as tab".Commendation
thanks for the input Dingle, I might be able to work around that somehow ;)Salve
@McBonio, Please update the post with your workaround method, thanks.Commendation
P
5

menubar must no, or 0, for Google Chrome to open in new window instead of tab.

Prizewinner answered 7/9, 2012 at 6:19 Comment(1)
This solution works in Google Chrome 29/Opera 20, although positioning windows in these two browsers (using "left=XX&top=YY") doesn't work as expected. (assumed to be a bug in both of these browsers)Nakitanalani
S
3

As far as I can tell, chrome doesn't work properly if you are referencing localhost (say, you're developing a site locally)

This works:

var windowObjectReference;
var strWindowFeatures = "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes";

function openRequestedPopup() {
  windowObjectReference = window.open("http://www.cnn.com/", "CNN_WindowName", strWindowFeatures);
}

This does not work

var windowObjectReference;
var strWindowFeatures = "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes";

function openRequestedPopup() {
  windowObjectReference = window.open("http://localhost/webappFolder/MapViewer.do", "CNN_WindowName", strWindowFeatures);
}

This also does not work, when loaded from http://localhost/webappFolder/Landing.do

var windowObjectReference;
var strWindowFeatures = "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes";

function openRequestedPopup() {
  windowObjectReference = window.open("/webappFolder/MapViewer.do", "CNN_WindowName", strWindowFeatures);
}
Sorilda answered 13/11, 2017 at 16:10 Comment(0)
S
2

Don't put a name for target window when you use window.open("","NAME",....)

If you do it you can only open it once. Use _blank, etc instead of.

Sulfuric answered 30/7, 2012 at 20:33 Comment(0)
S
0

Why is this still so complicated in 2021? For me I wanted to open in a new chrome window fullscreen so I used the below:

window.open("http://my.url.com", "", "fullscreen=yes");

This worked as exepected opening a new chrome window. Without the options at the end it will only open a new tab

Seleucid answered 11/2, 2021 at 10:6 Comment(0)
L
-3

I have the same problem, Chrome doesn`t open a new window from code (not handlers).
I found this solution:

setTimeout(function () {
    window.open(
        url, 'name', 'toolbar=1, scrollbars=1, location=1,
        statusbar=0, menubar=1, resizable=1, width=800, height=600'
    );
}, 1);
Luckey answered 19/12, 2017 at 15:42 Comment(3)
@cezar This is not a new question. Note the part that says "I found a this solution".Syncom
This creates an infinite loop. Moreover, it opens a new tab as opposed to a new window.Itu
Sorry. I am confused with two functions (SetInterval and SetTimeout), this is the solution that works with both, but SetInterval its realy infinity loop. As resut solution with SetInterval funtion it is wrong way, try use SetTimeout function for use this workarounLuckey

© 2022 - 2024 — McMap. All rights reserved.