I can't find a plausible explanation for the following behaviour of javascript code. I am not very familiar with javascript and if the following code was ever valid.
In a migrated legacy application there is code along those lines:
function myFunctionName(name)
{
for(k=0; k < document.budget_sections.length; ++k) {
//...do something
}
}
Please note the document.budget_sections.length
part. In production this code throws an error saying cannot access property length of null. In my dev environment this runs fine.
Since budget_sections
is the Id of a form element (<form id="budget_sections" method="post" runat="server">
)I changed the code to the syntax which is known to me:
function myFunctionName(name)
{
var form = document.getElementById("budget_sections");
for(k=0; k < form.length; ++k) {
//...do something
}
}
This syntax / notation is used throughout the whole application and works on most pages. The question for me here is: why did and still does this old code work and why might it break now? I have never read the notation document.DOM-object.xxx
before and I can't find documentation about it.