Javascript 谷歌浏览器“window.open”解决方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2572333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Chrome "window.open" workaround?
提问by Paul
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.
Chrome 似乎忽略了导致我出现问题的窗口功能,我正在努力解决的是我需要在新窗口中可以编辑地址栏。FF、IE、Safari 和 Opera 可以做到这一点,而 Chrome 则不然。
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;
}
回答by Jeroen
The other answers are outdated. The behavior of Chrome for window.opendepends on where it is called from. See also this topic.
其他答案已过时。Chrome for 的行为window.open取决于调用它的位置。另请参阅本主题。
When window.openis 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.openis called elsewhere, Chrome ignores other arguments and always opens a new window with a non-editable address bar.
当window.open从通过用户操作(例如 onclick 事件)触发的处理程序调用时,它的行为类似于<a target="_blank">,默认情况下在新选项卡中打开。但是,如果window.open在其他地方调用,Chrome 会忽略其他参数并始终打开一个带有不可编辑地址栏的新窗口。
This looks like some kind of security measure, although the rationale behind it is not completely clear.
这看起来像是某种安全措施,尽管其背后的基本原理尚不完全清楚。
回答by Ben
This worked for me:
这对我有用:
newwindow = window.open(url, "_blank", "resizable=yes, scrollbars=yes, titlebar=yes, width=800, height=900, top=10, left=10");
回答by scunliffe
The location=1part should enable an editable location bar.
该location=1部件应启用可编辑的位置栏。
As a side note, you can drop the language="javascript"attribute from your script as it is now deprecated.
作为旁注,您可以language="javascript"从脚本中删除该属性,因为它现在已被弃用。
update:
更新:
Setting the statusbar=1to the correct parameter status=1works for me
设置statusbar=1为正确的参数status=1对我有用
回答by Dingle
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.
我相信目前没有 javascript 方法可以强制 chrome 在选项卡模式下作为新窗口打开。已提交票证,如此处弹出窗口默认显示为选项卡。但是用户可以单击左上角的 chrome 图标并选择“显示为选项卡”,地址栏就可以编辑了。
A similar question asked in javascript open in a new window not tab.
回答by Tim Murphy
menubar must no, or 0, for Google Chrome to open in new window instead of tab.
菜单栏必须为 no 或 0,以便 Google Chrome 在新窗口而不是选项卡中打开。
回答by charley
As far as I can tell, chrome doesn't work properly if you are referencing localhost (say, you're developing a site locally)
据我所知,如果您引用 localhost(例如,您正在本地开发站点),则 chrome 无法正常工作
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
从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);
}
回答by Pedro
Don't put a name for target window when you use window.open("","NAME",....)
使用 window.open("","NAME",....) 时不要为目标窗口命名
If you do it you can only open it once. Use _blank, etc instead of.
如果你这样做,你只能打开它一次。使用 _blank 等代替。
回答by barsmaga
I have the same problem, Chrome doesn`t open a new window from code (not handlers).
I found this solution:
我有同样的问题,Chrome 不会从代码(不是处理程序)打开一个新窗口。
我找到了这个解决方案:
setTimeout(function () {
window.open(
url, 'name', 'toolbar=1, scrollbars=1, location=1,
statusbar=0, menubar=1, resizable=1, width=800, height=600'
);
}, 1);

