showModelessDialog - onLoad 失败 - IE 7

发布于 2024-07-10 11:37:53 字数 1125 浏览 4 评论 0原文

如果目标页面中有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒中人 2024-07-17 11:37:53

IE7 似乎显示了正确的行为。 HTML 是按顺序读取和解析的,包括脚本。 当解析器到达 javascript 警报时,它会执行它并等待返回。 然后,它可以完成页面解析并引发 onLoad 事件。

如果您希望在页面加载后显示警报,则必须处理 onLoad 事件本身。 您可以使用以下方式本地执行此操作:

window.onload = function() {
    //do stuff here
}

或者,您可以使用任意数量的 javascript 库(例如 jQuery)执行此操作:

$(document).ready(function() {
    //do stuff here
});

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:

window.onload = function() {
    //do stuff here
}

Or, you can do this with any number of javascript libraries, like jQuery:

$(document).ready(function() {
    //do stuff here
});
小…红帽 2024-07-17 11:37:53

您确定不是您的内联 onload 事件阻止了它吗? 下面的代码对我有用。

Index.htm

<html>
 <head>
  <title>Index</title>
  <script type="text/javascript" language="JavaScript">

   function openDialog() {
    if (window.showModalDialog)  { 
     var sURL = 'Modeless.htm';                       
     var oWin = window.showModelessDialog(sURL);      
    }
    else
    {
     alert('"showModlessDialog" not supported!');
    }
   }

   function addEventSimple(obj,evt,fn) {
    if (obj.addEventListener)
     obj.addEventListener(evt,fn,false);
    else if (obj.attachEvent)
     obj.attachEvent('on'+evt,fn);
    }

   function removeEventSimple(obj,evt,fn) {
    if (obj.removeEventListener)
     obj.removeEventListener(evt,fn,false);
    else if (obj.detachEvent)
     obj.detachEvent('on'+evt,fn);
    }

    addEventSimple(window, "load", openDialog);
  </script>
 </head>
 <body text="#ffffff">
  <h1 align="center">Index</h1>
 </body>
</html>

Modeless.htm

<html>
<head>
 <title>Modeless dialog</title>
  <script type="text/javascript" language="JavaScript">
   addEventSimple(window, "load", showAlert);

   function showAlert() {
    alert('Modeless is now Loaded');
   }

   function addEventSimple(obj,evt,fn) {
    if (obj.addEventListener)
     obj.addEventListener(evt,fn,false);
    else if (obj.attachEvent)
     obj.attachEvent('on'+evt,fn);
    }

    function removeEventSimple(obj,evt,fn) {
     if (obj.removeEventListener)
     obj.removeEventListener(evt,fn,false);
    else if (obj.detachEvent)
     obj.detachEvent('on'+evt,fn);
    }
  </script>
</head>
<body text="#ffffff" >
 <h1 align="center">Modeless</h1>
  <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>

注意:由于某种原因,我需要清除浏览器缓存才能更新对无模式窗口的任何更改。

Are you sure it's not your inline onload event that's stopping it? The code below works for me.

Index.htm

<html>
 <head>
  <title>Index</title>
  <script type="text/javascript" language="JavaScript">

   function openDialog() {
    if (window.showModalDialog)  { 
     var sURL = 'Modeless.htm';                       
     var oWin = window.showModelessDialog(sURL);      
    }
    else
    {
     alert('"showModlessDialog" not supported!');
    }
   }

   function addEventSimple(obj,evt,fn) {
    if (obj.addEventListener)
     obj.addEventListener(evt,fn,false);
    else if (obj.attachEvent)
     obj.attachEvent('on'+evt,fn);
    }

   function removeEventSimple(obj,evt,fn) {
    if (obj.removeEventListener)
     obj.removeEventListener(evt,fn,false);
    else if (obj.detachEvent)
     obj.detachEvent('on'+evt,fn);
    }

    addEventSimple(window, "load", openDialog);
  </script>
 </head>
 <body text="#ffffff">
  <h1 align="center">Index</h1>
 </body>
</html>

Modeless.htm

<html>
<head>
 <title>Modeless dialog</title>
  <script type="text/javascript" language="JavaScript">
   addEventSimple(window, "load", showAlert);

   function showAlert() {
    alert('Modeless is now Loaded');
   }

   function addEventSimple(obj,evt,fn) {
    if (obj.addEventListener)
     obj.addEventListener(evt,fn,false);
    else if (obj.attachEvent)
     obj.attachEvent('on'+evt,fn);
    }

    function removeEventSimple(obj,evt,fn) {
     if (obj.removeEventListener)
     obj.removeEventListener(evt,fn,false);
    else if (obj.detachEvent)
     obj.detachEvent('on'+evt,fn);
    }
  </script>
</head>
<body text="#ffffff" >
 <h1 align="center">Modeless</h1>
  <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>

Note: For some reason I need to clear my browser cache to get any changes to the modeless window to update.

白色秋天 2024-07-17 11:37:53

我认为在无模式对话框的 HTML 正文中使用alert() 存在一些混乱。
以下几点将更容易解释观察到的行为:

  1. 代码检查是否支持非模式对话框(对象检测)
  2. 如果是,则在上述检查中,然后继续下一步,否则显示警报。
  3. 在模式对话框中,HTML 正文由浏览器下载并按顺序解析,如
  4. 脚本标签可以出现在正文中的任何位置,并将由 MS Windows Script Host(MSIE 的 JavaScript 引擎)解析和执行。 由于模式对话框中的警报不存在于函数中,因此它不再被视为全局代码块,并且当 JS 引擎执行脚本块时将立即执行。
  5. 警报会停止 JavaScript 的任何进一步执行。 仅当用户消除警报时,JavaScript 才会恢复执行。
  6. 仅当文档完全下载并呈现时才会触发 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:

  1. The code checks for support of modeless dialog (Object detection)
  2. If yes, in the above check, then proceed to the next step, else display alert.
  3. In the modal dialog, the HTML body is downloaded by the browser and is parsed sequentially, as pointed out by in the other answer.
  4. Script tags can appear anywhere in the body, and will be parsed and executed by MS Windows Script Host (the JavaScript engine for MSIE). Since the alert in the modal dialog is not present in a function, it ends being treated as global block of code, and will be executed immediately, when execution of the script block is done by the JS engine.
  5. Alerts halt any further execution of JavaScript. Execution of JavaScript will resume only when the user dismisses the alert.
  6. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文