views:

1355

answers:

3

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

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.

w4g3n3r
+1  A: 

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
});
EndangeredMassa
A: 

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.

Vineet Reynolds