Javascript 的 window.open 函数不一致,未按预期打开弹出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10086811/
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's window.open function inconsistent, does not open pop up when expected
提问by s2cuts
Ok, I'm going nuts here. I'm trying to implement a basic popup window to display images. If my javascript code is fully specified in an HTML tag with the onclick property, the window pops up correctly. If my IDENTICAL javascript code is called from either the script tag at the beginning of the HTML document or from a separate js file, the link (or image in my case) opens not in a popup, but in the same window. < frustration >
好吧,我要疯了。我正在尝试实现一个基本的弹出窗口来显示图像。如果我的 javascript 代码在具有 onclick 属性的 HTML 标记中完全指定,则窗口会正确弹出。如果我的 IDENTICAL javascript 代码是从 HTML 文档开头的脚本标记或从单独的 js 文件调用的,则链接(或在我的情况下为图像)不会在弹出窗口中打开,而是在同一窗口中打开。<沮丧>
This opens a popup:
这会打开一个弹出窗口:
<a href="test.jpg" onclick="window.open('test.jpg','test_name','width=500,height=500,scrollbars=no'); return false">test_name</a>
This does not open a popup:
这不会打开弹出窗口:
function cleanPopup(url, title) {
window.open(url, title, 'width=500,height=500,scrollbars=no');
return false;
}
<a href="test.jpg" onclick="return cleanPopup('test.jpg', 'test_name')">test_name</a>
Any help would be appreciated.
任何帮助,将不胜感激。
PS: Tested in Chrome and Firefox.
PS:在 Chrome 和 Firefox 中测试。
EDIT:
编辑:
I've discovered my problem. I originally only called the js file in the head tag. There is something about the layers of div's when created with multiple scripts of a templating tool (Template Toolkit in my case) that makes the original script element within the head element seemingly invisible to the deeper child elements.
我发现了我的问题。我最初只调用了head标签中的js文件。当使用模板工具(在我的例子中是模板工具包)的多个脚本创建 div 层时,有一些关于 div 层的东西,这使得 head 元素中的原始脚本元素对于更深层的子元素似乎不可见。
I do not know exactly what's going on, and I don't have the time to explore it. I've added this edit just in case some other person has a similar issue and somehow stumbles across this thread. If someone understands this phenomenon, and can explain it, please do.
我不知道到底发生了什么,也没有时间去探索。我添加了这个编辑,以防万一其他人有类似的问题并且不知何故偶然发现了这个线程。如果有人理解这种现象,并能解释它,请做。
EDIT 2:
编辑2:
The "name" parameter of the window.open method can not contain spaces for the popup to work in IE. Thanks M$.
window.open 方法的“name”参数不能包含空格,以便弹出窗口在 IE 中工作。谢谢百万美元。
采纳答案by mplungjan
This code is the closest to foolproof you can get in my opinion.
在我看来,这段代码是最接近万无一失的。
Tested on
经过测试
- Windows - Fx, Chrome, IE8 and Safari
- iPhone: Mobile Safari
- Windows - Fx、Chrome、IE8 和 Safari
- iPhone:移动版 Safari
- adding a target makes the link open in a new window or tab if allowed - in case the script fails for any reason - this is a SIDE-EFFECT which is useful but not the answer to the question.
- returning true if the window.open fails will also make the click follow the link, hopefully invoking the target - Note that some browsers no longer reacts to target.
- the height (and width) in the 3rd parameters will enforce the opening in a new window rather than a new tab unless the browser is set to open all new windows in a tab
- 如果允许,添加目标会使链接在新窗口或选项卡中打开 - 以防脚本因任何原因失败 - 这是一个有用的副作用,但不是问题的答案。
- 如果 window.open 失败,则返回 true 也会使点击跟随链接,希望调用目标 - 请注意,某些浏览器不再对目标做出反应。
- 第三个参数中的高度(和宽度)将强制在新窗口而不是新选项卡中打开,除非浏览器设置为在选项卡中打开所有新窗口
<html>
<head>
<script type="text/javascript">
window.onload=function() {
document.getElementById('popup').onclick=function() {
var w = window.open(this.href, this.target,
'width=500,height=500,scrollbars=no');
return (!w); // opens in new window/tab if allowed
}
}
</script>
</head>
<body>
<a id="popup" href="test.jpg" target="test_name">test_name</a>
</body>
</html>
回答by BiAiB
use target="_blank"
使用目标=“_blank”
<a href="whatever" target="_blank"> ...
from HTML, or
来自 HTML,或
window.open(url, '_blank', options)
from javascript
来自 javascript
回答by Yektaweb Company
Make new window title random
使新窗口标题随机
onclick="PopupCenter(this.href,'random','600','700','yes'); return false;"
and in function:
并在功能上:
if(title=='random')
{
title = randomID();
}
回答by mintokyo
try this
试试这个
function mypopup()
{
mywindow = window.open("http://www.test.com", "mywindow", "location=1,status=1,scrollbars=1, width=100,height=100");
}
回答by sowrov
Make sure you put your javascript code in the Head block
确保将 javascript 代码放在 Head 块中
<head>
<script type="text/javascript" language="JavaScript">
function cleanPopup(url, title) {
window.open(url, title, 'width=500,height=500,scrollbars=no');
return false;
}
</script>
And in the body:
在体内:
<a href="" onclick="cleanPopup('test.jpg','test_name')">test_name</a>
It just work for me without any problem both in Firefox and IE
它对我来说在 Firefox 和 IE 中都没有任何问题