I'm using the provided addLoadEvent javascript function to hook in a function that I'm using to preload some images.

// addLoadEvent is provided by drupal
if (isJsEnabled()) {
   addLoadEvent(MM_preloadImages('image1.jpg','image1.jpg','image3.jpg');
}

Works fine in Firefox, but when viewing under IE an error dialog pops up
---------------------------
Error
---------------------------
A Runtime Error has occurred.
Do you wish to Debug?

Line: 185
Error: Object expected
---------------------------
Yes No
---------------------------

of /misc/drupal.js

/**
 * Adds a function to the window onload event
 */
function addLoadEvent(func) {
  var oldOnload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  }
  else {
    window.onload = function() {
      oldOnload();
      func();
    }
  }
}

line 185 is the func() call in the else block.

I looked around on the forums but did not find any other reports of issues with IE.

Just in case, here is the code for the hooked js function

function MM_preloadImages() { //v3.0
  var d=document; 
  if(d.images) { 
    if(!d.MM_p) 
      d.MM_p=new Array();
      var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
      for(i=0; i<a.length; i++)
        if (a[i].indexOf("#")!=0){ 
            d.MM_p[j]=new Image; 
            d.MM_p[j++].src=a[i];
        }
      }
}

Comments

Steven’s picture

Status: Active » Closed (works as designed)

You forgot a bracket at the end of the addLoadEvent() call.

emackn’s picture

whoops.. I deleted a paren by accident formatting the code. But the original code syntax is correct.

Steven’s picture

addLoadEvent's argument is a function.

What you're doing is calling MM_preloadImages(...) first, and then passing that to addLoadEvent. You can get around this by using an anonymous function:

if (isJsEnabled()) {
  addLoadEvent(function() {
    MM_preloadImages('image1.jpg','image1.jpg','image3.jpg');
  });
}
emackn’s picture

Thanks, That makes sense.

Should all JS functions be added anonymously to addLoadEvent? If so, this example could be noted on http://drupal.org/node/22218#hook_onload to remedy some confusion. ;)