admin管理员组

文章数量:1402995

I have a method that is instantiated on page load. The method fires two events, window.onload and image.onclick. The problem is only one of the event works at any given time (i.e.: if I ment image.onclick method, window.onload method works). How do i get both to work?

This works fine in Firefox (Mozilla) but not in IE

Example

test.method = function()
{
    window.onload = this.doSomeWork;

    for (i = 0; i < images.length; i++) {
        var image = images[i];
        image.onclick = this.imageClickEvent;

    }
}

I have a method that is instantiated on page load. The method fires two events, window.onload and image.onclick. The problem is only one of the event works at any given time (i.e.: if I ment image.onclick method, window.onload method works). How do i get both to work?

This works fine in Firefox (Mozilla) but not in IE

Example

test.method = function()
{
    window.onload = this.doSomeWork;

    for (i = 0; i < images.length; i++) {
        var image = images[i];
        image.onclick = this.imageClickEvent;

    }
}
Share Improve this question edited Dec 26, 2022 at 22:10 munity wiki
6 revs, 3 users 70%
DK. 1
  • "This works fine in Firefox (mozilla) but not in IE" -- I wish I had a nickel for every time someone said that... :D – BoltBait Commented Feb 27, 2009 at 19:57
Add a ment  | 

5 Answers 5

Reset to default 1

The problem us that you can only have one onload event.

I remend that yuu should try the addEvent method found here:

http://ejohn/projects/flexible-javascript-events/

There's not really enough information in the question to figure out what's going wrong, but things to look at include:

  • is the code in the doSomeWork() or imageClickEvent() expecting to receive a ‘this’ that points to anything in particular? Because it won't, when you peel a method off its owner object to assign to an event handler.

  • is there another method writing to window.onload, or image.onclick, that you might be overriding?

after you fire the test.method on page load, you overwrite your window.onload with a new value: this.dosomework, so within the time frame of the body load, you call the first function, then immidiately overwrite, then second function takes over, and carries out... did you try to push the window.onload statement way to the bottom of the test.method? my guess if the function block is too long, the second window.onload is never gonna be carried out, because the window has already finished loading, its milliseconds, but thats all it takes

apparently in firefox the function is carried out before it gets overwritten, or maybe appeneded to the current onload, not sure about technicalities. again, try to push onload to the bottom and see what happens

you should not really call an onload function within any other function that is called after body load, because no matter how careful you are you cannot depend on the fact that the loading time frame is long enough to contain both events, you should append to window.onload before the body tag is called (or finished) to make sure the event is caught.

Look at the code on a page with Firefox 3. Then open the Firefox Error Console (Ctrl Shift J) to see which line is breaking. Then update your question with the new info.

Perhaps you should be adding an event listener here, and not trying to capture window.onload?

本文标签: javascriptonloadonclick eventsStack Overflow