Chris
Chris

Reputation: 28064

JavaScript - override or prevent execution

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?

Upvotes: 6

Views: 3389

Answers (7)

Ian Oxley
Ian Oxley

Reputation: 11056

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();
});

Upvotes: 0

TM.
TM.

Reputation: 111027

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

Upvotes: 0

Ayyash
Ayyash

Reputation: 4399

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;

Upvotes: 0

user64417
user64417

Reputation:

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.

Upvotes: 0

AnthonyWJones
AnthonyWJones

Reputation: 189457

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.

Upvotes: 1

Joshua
Joshua

Reputation: 43278

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

Upvotes: 0

user64417
user64417

Reputation:

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.

Upvotes: 1

Related Questions