8

Possible Duplicate:
JavaScript Function Syntax Explanation: function object.myFunction(){..}

I've seen some (legacy) javascript code recently that looks like:

function window.onload(){
  // some code
}

This doesn't look like valid javascript to me since you can't have a period in an identifier, but it seems to work in IE8. I assume it's the equivalent of:

window.onload = function(){}

I've tried the same code in Chrome and IE9 and both of them raise syntax exceptions, so am I correct in thinking that this "feature" of IE8 is some non-standard function definition that should be replaced? The code in question is only sent to IE browsers, so that's probably why I haven't run into this issue before.

Community
  • 1
  • 1
nemec
  • 1,044
  • 9
  • 35

2 Answers2

2

For Javascript on recent browsers, you can usually refer to ECMAScript and clearly, this isn't allowed in ECMAScript :

ECMAScript spec on function definition :

The production FunctionDeclaration : function Identifier ( FormalParameterListopt ){ FunctionBody } [...]

Create a property of the current variable object (as specified in 10.1.3) with name Identifier

SO on valid names (just in case somebody would think "window.onload" is the name of the function, which thus would be window.window.onload)

Community
  • 1
  • 1
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Thanks! I wanted to get a second opinion before bringing it up to the original developer. – nemec Oct 08 '12 at 15:50
  • I'd hope the fact that his way of defining functions isn't compatible with IE9, Chrome, Safari, Firefox or Opera would be enough to convince him, even without the link to the standards. – Denys Séguret Oct 08 '12 at 15:53
  • Well this code is only being executed in IE and it predates IE9 so I'm only moderately surprised that it's been missed. – nemec Oct 08 '12 at 16:09
0

You can define new functions using this syntax:

function myFunc(args)
{
  ...
}

The window.onload function is defined by the browser, and may be null. The period means that the onload value is a member of the window object. The following syntax will take a function and assign it to the value of the onload member:

window.onload = function()
{
  ...
};

Don't forget the trailing semicolon, since this is an assignment statement.

Zach Rattner
  • 20,745
  • 9
  • 59
  • 82
  • 1
    You may need to take another look at the question. OP is asking about this syntax, which is invalid, but works in IE8: `function window.onload(){` – I Hate Lazy Oct 08 '12 at 15:41
  • Right, you shouldn't do that for members that are already declared. – Zach Rattner Oct 08 '12 at 15:42
  • It doesn't really have anything to do with declared/undeclared members. The function syntax being used is invalid JavaScript altogether. It gives a *SyntaxError* because of the `.` in the name. But somehow it works in IE. – I Hate Lazy Oct 08 '12 at 15:47
  • 1
    Yeah, my question was more "is there a reason this works in IE?" – nemec Oct 08 '12 at 15:54
  • My point was that it shouldn't work (who knows why ie doesn't care?) and I tried to explain why. Sorry if you didn't find it useful. – Zach Rattner Oct 08 '12 at 17:05