I would consider myself to be reasonably competent with JavaScript and familiar with many of the different ways of achieving the same thing. But today I came across some function syntax which I hadn't seen before:
function document.body.onload()
{
alert('loaded');
}
If I were to write such code I would have done it like this:
document.body.onload = function()
{
alert('loaded');
}
Ignoring the fact that this is not the best way to handle the onload
event, is this actually valid JavaScript? It appears to causes syntax errors in FireFox (and JSLint), so I am guessing that it is Internet Explorer only syntax? If it is IE only then I would like to remove it but I am concerned that it might have some quirky side effect.
function document.body.onload
is non-standard JavaScript syntax. Use the second format, and fire whomever wrote the original code.
}
document.body.onload = function () {
...code...
}; //this is easy to miss
No, it's not valid. the function X() {}
variant requires that the name of the function only contains letters, digits, '$' and '_'.
If you want to check other syntax, you can get the standard here, but the syntax diagrams in the back of JavaScript: The Good Parts are much easier to digest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With