Jamex
Jamex

Reputation: 1222

firefox automatically triggers javascript function named 'onload'

Can someone confirm that firefox (3.6) automatically runs the javascript function named 'onload' without explicitly being called? Chrome and IE do not automatically run a declared function unless called, but firefox apparently will run the declared function 'onload' even if it is not called (all in lowercases).

Here is the jsfiddle link to test.

In the body tag, if you assign the function named 'test' to onload event, then firefox will call the test function. If you remove the onload call, firefox will call the function 'onload' automatically.

Is this a known property of firefox?

Upvotes: 4

Views: 483

Answers (2)

Boris Zbarsky
Boris Zbarsky

Reputation: 35074

Firefox 3.6 does in fact do that. So do 4-8. Firefox 9 fixes this bug, so that function onload() {} no longer adds the function as a load event listener. See https://bugzilla.mozilla.org/show_bug.cgi?id=659350 for the details of that change.

Upvotes: 3

Tomas
Tomas

Reputation: 59515

It's because if you declare a global function onload(), it's in fact window.onload. This example explains it:

<script>
var a = 1;
alert(window.a); // alerts "1"
</script>

This should be cross-browser valid (I tested it only on FF3.6 and IE7,8 though).

Upvotes: 3

Related Questions