Javascript 的 window.open 函数不一致,未按预期打开弹出 [英] Javascript's window.open function inconsistent, does not open pop up when expected

查看:26
本文介绍了Javascript 的 window.open 函数不一致,未按预期打开弹出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我要疯了.我正在尝试实现一个基本的弹出窗口来显示图像.如果我的 javascript 代码在具有 onclick 属性的 HTML 标记中完全指定,则窗口会正确弹出.如果我的 IDENTICAL javascript 代码是从 HTML 文档开头的脚本标记或从单独的 js 文件调用的,则链接(或在我的情况下为图像)不会在弹出窗口中打开,而是在同一窗口中打开.<沮丧>

这会打开一个弹出窗口:

<a href="test.jpg" onclick="window.open('test.jpg','test_name','width=500,height=500,scrollbars=no'); return false">test_name</a>


这不会打开弹出窗口:

function cleanPopup(url, title) {window.open(url, title, 'width=500,height=500,scrollbars=no');返回假;}<a href="test.jpg" onclick="return cleanPopup('test.jpg', 'test_name')">test_name</a>


任何帮助将不胜感激.

PS:在 Chrome 和 Firefox 中测试.

我发现了我的问题.我最初只在 head 标签中调用了 js 文件.当使用模板工具(在我的例子中是模板工具包)的多个脚本创建 div 层时,有一些关于 div 层的东西,这使得 head 元素中的原始脚本元素对于更深层的子元素似乎不可见.

我不知道到底发生了什么,也没有时间去探索.我已经添加了这个编辑,以防万一其他人有类似的问题并且不知何故偶然发现了这个线程.如果有人理解这种现象,并能解释它,请做.

编辑 2:

window.open 方法的name"参数不能包含空格,以便弹出窗口在 IE 中工作.谢谢 M$.

解决方案

DEMO HERE

在我看来,此代码最接近万无一失.

经过测试

  • Windows - Fx、Chrome、IE8 和 Safari
  • iPhone:移动版 Safari

  1. 添加目标会使链接在允许的情况下在新窗口或选项卡中打开 - 以防脚本因任何原因失败 - 这是一个有用的副作用,但不是问题的答案.
  2. 如果 window.open 失败返回 true 也会使点击跟随链接,希望调用目标 - 请注意,某些浏览器不再对目标做出反应.
  3. 第三个参数中的高度(和宽度)将强制在新窗口而不是新选项卡中打开,除非浏览器设置为在选项卡中打开所有新窗口

<头><script type="text/javascript">window.onload=function() {document.getElementById('popup').onclick=function() {var w = window.open(this.href, this.target,'宽度=500,高度=500,滚动条=无');返回(!w);//如果允许,在新窗口/选项卡中打开}}<身体><a id="popup" href="test.jpg" target="test_name">test_name</a></html>

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 >

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.

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.

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:

The "name" parameter of the window.open method can not contain spaces for the popup to work in IE. Thanks M$.

解决方案

DEMO HERE

This code is the closest to foolproof you can get in my opinion.

Tested on

  • Windows - Fx, Chrome, IE8 and Safari
  • iPhone: Mobile Safari

  1. 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.
  2. 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.
  3. 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

<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>

这篇关于Javascript 的 window.open 函数不一致,未按预期打开弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆