Closed (works as designed)
Project:
Drupal core
Version:
4.7.2
Component:
javascript
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
5 Jul 2006 at 15:28 UTC
Updated:
7 Jul 2006 at 13:35 UTC
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
Comment #1
Steven commentedYou forgot a bracket at the end of the addLoadEvent() call.
Comment #2
emackn commentedwhoops.. I deleted a paren by accident formatting the code. But the original code syntax is correct.
Comment #3
Steven commentedaddLoadEvent'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:
Comment #4
emackn commentedThanks, 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. ;)