Javascript 弹出窗口不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10567973/
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
Javascript popup not working
提问by sears
Whats wrong with the below page?
下面的页面有什么问题?
<html>
<head>
<script type="text/javascript">
function openWin(url, w, h)
{
var left = (screen.width/2)-(w/2);
var top = (screen.height/2)-(h/2);
w=window.open(url, '_blank', 'width='+w+', height='+h+', scrollbars=no, menubar=no, resizable=no, location=no, toolbar=no, top='+top+', left='+left+', 'false');
w.focus();
}
</script>
</head>
<body>
<input type="button" value="Open window" onclick="openWin(about:blank, 200, 200)" />
</body>
</html>
Whenever I click the button nothing happens.
每当我单击按钮时,什么也没有发生。
Using chrome
使用铬
回答by glarkou
Try this:
试试这个:
<html>
<head>
<script type="text/javascript">
function openWin(url, w, h)
{
var left = (screen.width/2)-(w/2);
var top = (screen.height/2)-(h/2);
w=window.open(url, '_blank', width='+w+', height='+h+', scrollbars='no', menubar='no', resizable='no', location='no', toolbar='no', top='+top+', left='+left+', 'false');
w.focus();
}
</script>
</head>
<body>
<input type="button" value="Open window" onclick="openWin('about:blank', 200, 200); return false;" />
</body>
</html>
回答by Musa
you Have 2 syntax errors, one in your onClick attribute, your about:blank
isn't quoted
Two, in window.open you have +'
at the end of you second argument also you have the third argument as a string, it should be boolean.
你有 2 个语法错误,一个在你的 onClick 属性中,你about:blank
没有引用
两个,在 window.open 中,你有+'
第二个参数的末尾,第三个参数是一个字符串,它应该是布尔值。
回答by Cris Mooney
A unique bit of relevant trivia:
at least in recent releases of IE, 9.0.8112.16241 (vista/win7) and 8.0.6001.18072 (xp), programmatically do a window.open(‘about:blank')
and the programmable focus()
will be stuck in the address bar showing “about:blank”.
Note:window.open(”)
with an empty string URL is the same as window.open(‘about:blank')
.
一个独特的相关琐事:
至少在最近版本的 IE、9.0.8112.16241 (vista/win7) 和 8.0.6001.18072 (xp) 中,以编程方式执行 awindow.open(‘about:blank')
并且可编程focus()
将卡在地址栏中显示“ about:blank” .
注意:window.open(”)
带有空字符串的 URL 与window.open(‘about:blank')
.
Adding the option location=no
works around this based on IE security settingAllow websites to open windows without address or status bars
添加该选项location=no
可根据 IE 安全设置解决此问题Allow websites to open windows without address or status bars
However they default badly (force address bar). Simplest I've found is to have users set our site in their security settings as “trusted” where the default “medium” settings do not force the address bar to show.
然而,它们的默认设置很糟糕(强制地址栏)。我发现的最简单的方法是让用户在他们的安全设置中将我们的站点设置为“受信任”,其中默认的“中等”设置不会强制显示地址栏。
Using a dummy URI javascript:popup=1
in IE 8 did work around this, but that may open in a new tab based on settings which may not work for you.
javascript:popup=1
在 IE 8 中使用虚拟 URI确实解决了这个问题,但它可能会根据可能不适用于您的设置在新选项卡中打开。
Relevant custom security setting:
相关自定义安全设置:
- Where: Tool->Internet Options->Security->Custom Level->Miscellaneous
- Value: Allow websites to open windows without address or status bars: enable
- 其中:工具->Internet 选项->安全->自定义级别->其他
- 值:允许网站打开没有地址栏或状态栏的窗口:启用
Note:mine is a legacy web based “application” that employs the legitimately useful design of popups which are beneficial (no one has ripped them out of “Word” yet). It is enough challenge for those of us damaged as this useful UI mechanism has been marred by scammers and hackers that I implore you to resist further beating us up with the non-productive “don't use popups” tangential interjection.
注意:我的是一个传统的基于网络的“应用程序”,它采用了合法有用的弹出窗口设计,这些设计是有益的(还没有人将它们从“Word”中删除)。对于我们这些受到损害的人来说,这是足够的挑战,因为这种有用的 UI 机制已被骗子和黑客破坏,我恳请您不要再用非生产性的“不要使用弹出窗口”切向感叹词来打击我们。
Test code (onloadJSServerSidePage
works, onloadJSClientSidePage
has issues):
测试代码(onloadJSServerSidePage
有效,onloadJSClientSidePage
有问题):
http://forus.com/csm/code/iepopup/testPopupFocus.html
http://forus.com/csm/code/iepopup/testPopupFocus.html
Optional “testPopupFocusContent.html” for onloadJSServerSidePag
:
可选的“testPopupFocusContent.html”用于onloadJSServerSidePag
:
http://forus.com/csm/code/iepopup/testPopupFocusContent.html
http://forus.com/csm/code/iepopup/testPopupFocusContent.html
回答by Sanath
here the last argument was malformed and corrected removing a '
此处最后一个参数格式错误并已更正删除 '
w=window.open(url, '_blank', 'width='+w+', height='+h+', scrollbars=no, menubar=no,
resizable=no, location=no, toolbar=no, top='+top+', left='+left+', false');
in html calling code,
在 html 调用代码中,
<input type="button" value="Open window" onclick="openWin('http://www.google.com', 200, 200)" />
in the url part you can have an either absolute url or relative one..
在 url 部分,您可以拥有绝对 url 或相对 url。
hope this helps..
希望这可以帮助..
回答by totallyNotLizards
Try this instead:
试试这个:
<input type="button"
value="Open window"
onclick="openWin('about:blank', 200, 200);" />
I think the colon in the URL param in your function call is causing the problem - if it's a string it should be in quotes.
我认为函数调用中 URL 参数中的冒号导致了问题 - 如果它是一个字符串,它应该用引号引起来。
EDIT: added a semi colon - this seems to work for me.
编辑:添加了一个分号 - 这似乎对我有用。
回答by Marc
Your false
at the end of the w=window.open()
line isn't properly quoted/concatenated.
您false
在该w=window.open()
行的末尾未正确引用/连接。