views:

600

answers:

7

I'm working on a client project and I have to include their header and footer, which includes some core javascript files. I have a couple of PNGs on the page, but their core JS file is poorly coded and doesn't check for IE 7 before attempting to replace IMG tags that contain .png files with DIVS that use the AlphaImageLoader filter. The result is that in IE 7, all my .png images are replaced with DIV tags that have a default display: block, causing a linebreak after every single png image in my pages.

What I'd like to do is override their function with a better one or somehow prevent theirs from executing, but I cannot modify the js file itself, which both defines the function and attaches it to the window onload event. I've tried redefining the function under the same name in several places (header, just before the /body tag, in $(document).ready, etc...) but the original function always seems to execute, presumably because the original function code is what is stored with the event handler, and not merely a pointer to the function.

Any way I can fix? Is there a way to selectively remove onload event handlers?

+1  A: 

If that's the only thing running at load, I think you could do

window.onload = null;

If there are other things running, I guess you'd have to reattach them. It's a little fragile, I suppose.

Burke
Yeah the problem is that I don't know what else is being attached, and cannot make any guarantees.
Chris
Is there a way to enumerate all attached events?
Chris
@Chris: No there isn't
AnthonyWJones
Hrm, about all I can suggest then is to fire up Firebug or something, and have a look at window.onload. There may be some obvious way to change it.
Burke
A: 

A large IFRAME between header & footer should do the trick.

Joshua
+1  A: 

In IE7 you can use the detachEvent method:-

window.detachEvent("load", fn)

where fn is the function that was attached, however since there is jquery in this mix it may be a tall order getting hold of the actual function that was attached. Most likely the actual function attached will be anonymous.

AnthonyWJones
using 'load' threw an error, but 'onload' did not. Neither stopped the function from executing, however.
Chris
@Chris: I couldn't remember whether IE uses the "on" prefix in this case. The reason its not working will be because the function in question is either called by another function which is attached to the event or call by a function assigned directly to the onload property.
AnthonyWJones
It might help if you add a little bit of code or event a small repro of what you are trying to do to the question.
AnthonyWJones
It's possible the function is being assigned anonymously or through a proxy. If it's been attached with attachEvent, detachEvent really should work for a known function. And yes IE prefixes with "on".
annakata
A: 

Well, depending on how it was bound, you might be able to get away with something like:

window.onload = function(){
  var alert=function(a){
    console.log(a);
  }
  window.onload();
}

Obviously, you'd want to redefine something other than alert, but that might work.

Burke
Nope, didn't work
Chris
A: 

maybe if that's all it does, you can write a function to reverse it, look for all png images and strip away the div, and if you want to skip certain images you can implant an attribute to those you want to treat differently

another way is to trick the function by not having the png part of the image file name, and on load, append the .png (after their onload)

or maybe you can replace your png images with another tag, and replace onload

by the way, you can know exactly whats inside the onload, if you just alert window.onload, if there is nothing but that functionality, set window.onload = null;

Ayyash
Just FYI, setting window.onload to null doesn't work, at least for IE 7 which is the problem case. The assigned event handlers still fire.
Chris
what about window.onload = function(){ };if that does not prevent the function, then its not onload you should fix, maybe its document.body.onload, and it may also be onload of a specific element, like an empty image...
Ayyash
A: 

Have you tried using $(window).unbind("load")?

TM
A: 

Do you know the name of the function that replaces the PNG images? If so you might be able to override the existing function by doing something like this:

// Assuming PNG function is called pngSwap
function pngSwap() {
 alert('png swap');    
}

$(document).ready(function() {
 if (window.pngSwap && window.pngSwap.constructor === Function) {
  var oldFunc = window.pngSwap;
  window.pngSwap = function() {
   alert('new png swap');
  }
 }

 pngSwap();
});
Ian Oxley