showModelessDialog - onLoad 失败 - IE 7
如果目标页面中有 Javascript 警报,MSIE v7 不会(在我手中)打开无模式对话框或触发 onLoad 事件。 以下内容在 MSIE v7 中失败,但在 v6 中正常(如果需要,可以使用完整源的 zip 文件)。
希望其他人确认这一点并讨论为什么会这样。
index.htm(此处仅显示 javascript 函数)
function openDialog(n) {
if (typeof(window.showModalDialog) == 'object') { /* Ensure of browser support */
var sURL = 'modeless.htm'; /* Set the URL */
var oWin = window.showModelessDialog(sURL); /* Create new modeless window */
}
else {
alert('"showModlessDialog" not supported!');
}
}
modeless.htm
<html>
<head>
<title>Modeless dialog</title>
</head>
<body bgcolor="#ff0000" text="#ffffff" onLoad="alert('Modeless is now loaded')">
<center>
<h1>Modeless</h1>
</center>
<script type="text/javascript" language="JavaScript">
/* If the next line is included, it prevents the onLoad event occurring in MSIE v7 */
alert('This alert stops the onLoad event in MSIE v7!');
</script>
</body>
</html>
MSIE v7 does not (in my hands) open a Modeless Dialog or trigger an onLoad event if there is a Javascript alert in the target page. The following fails in MSIE v7 but is OK in v6 (zip file of full source available if required).
Would appreciate others confirming this and discussing why this should be so.
index.htm (only javascript function shown here)
function openDialog(n) {
if (typeof(window.showModalDialog) == 'object') { /* Ensure of browser support */
var sURL = 'modeless.htm'; /* Set the URL */
var oWin = window.showModelessDialog(sURL); /* Create new modeless window */
}
else {
alert('"showModlessDialog" not supported!');
}
}
modeless.htm
<html>
<head>
<title>Modeless dialog</title>
</head>
<body bgcolor="#ff0000" text="#ffffff" onLoad="alert('Modeless is now loaded')">
<center>
<h1>Modeless</h1>
</center>
<script type="text/javascript" language="JavaScript">
/* If the next line is included, it prevents the onLoad event occurring in MSIE v7 */
alert('This alert stops the onLoad event in MSIE v7!');
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
IE7 似乎显示了正确的行为。 HTML 是按顺序读取和解析的,包括脚本。 当解析器到达 javascript 警报时,它会执行它并等待返回。 然后,它可以完成页面解析并引发 onLoad 事件。
如果您希望在页面加载后显示警报,则必须处理 onLoad 事件本身。 您可以使用以下方式本地执行此操作:
或者,您可以使用任意数量的 javascript 库(例如 jQuery)执行此操作:
It appears that IE7 is displaying the proper behavior. HTML is read and parsed sequentially, including scripts. When the parser reaches the javascript alert, it executes it and waits for a return. Then, it can finish parsing the page and raise the onLoad event.
If you want the alert to be displayed after the page has been loaded, you must handle the onLoad event itself. You can do this natively with:
Or, you can do this with any number of javascript libraries, like jQuery:
您确定不是您的内联 onload 事件阻止了它吗? 下面的代码对我有用。
Index.htm
Modeless.htm
注意:由于某种原因,我需要清除浏览器缓存才能更新对无模式窗口的任何更改。
Are you sure it's not your inline onload event that's stopping it? The code below works for me.
Index.htm
Modeless.htm
Note: For some reason I need to clear my browser cache to get any changes to the modeless window to update.
我认为在无模式对话框的 HTML 正文中使用alert() 存在一些混乱。
以下几点将更容易解释观察到的行为:
仅当文档完全下载并呈现时才会触发 onload 处理程序。 因此,警报的执行将延迟 onload 处理程序的执行,直到用户解除警报,并且文档的其余部分被解析和呈现。
Opera 开发者社区关于计时和同步的文章JavaScript(尽管它没有专门讨论 IE)在这种情况下是一篇非常有用的文章。
更新:我尝试在服务器(Apache Tomcat)和文件系统上运行类似的代码。 看起来当我从文件系统而不是从服务器打开 index.html 时,会发生所描述的行为。 IE 的区域设置可能在这里起作用。
I think there is some confusion over the use of alert() in the HTML body of the modeless dialog.
The following points will make it easier to explain the observed behavior:
The onload handler is fired only when the document has been completely downloaded and rendered. Hence the execution of the alert will delay the execution of the onload handler until the user dismisses the alert, and the remainder of the document is parsed and rendered.
The Opera Developer Community article on timing and synchronization in JavaScript (even though it does not talk of IE specifically) is a really useful article to read, in this context.
Update: I tried running similar code, both off a server (Apache Tomcat), and off the file system. Looks like the behavior described occurs when I open index.html from the filesystem, and not from the server. IE's zone settings might be at work here.