Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between document.all and document.forms[0]

Tags:

javascript

Can any one explain what is the difference between

document.all

and the document.forms[0] please?

Thank you

like image 853
Mihir Avatar asked Feb 25 '23 14:02

Mihir


1 Answers

document.all gives you a reference to an array-like object containing all elements of the document in Internet Explorer (IE). Document.forms[0] gives you a pointer to the first form element in the document, in all browsers

The 2 are quite different then. If your form had a name attribute, say 'myform', then in IE that form could be referenced with document.all.myform

document.all is deprecated from IE version 5 and up. You can still use it though, even in IE9 it's still available. Often it's used to test if the browser is IE:

if (document.all) {
  //o no, it's IE again! We have to do it another way!
}

Referencing forms in the form of document.forms[0] is considered bad practice. More on that can be found here

NOTE: Since this answer was first written IE11 has been introduced which dropped support for document.all See Compatibility changes in IE11 for more information

like image 91
KooiInc Avatar answered Mar 06 '23 22:03

KooiInc