Check typeof length instead of instanceof
Change the var decl to func and remove direct reference to Element in IE<=8