Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

[Google Translate]


I made a page with only the following:
<input id = cpf value = "anything">

before the </ body>
<script>
console.log (cpf.value); // "Anything"
</ script>
tested in chrome 36; IE11 and latest version firefox and it worked in all of them.
Because this work?
This makes obsolete document.getElementById?


[Original]


(Title: Porquê este código funciona?)

Eu fiz uma página somente com o seguinte:
<input id=cpf value="qualquer coisa">

antes do </body>
<script>
console.log(cpf.value); // "qualquer coisa"
</script>
testei no chrome 36; IE11 e firefox ultima versão e funcionou em todos eles.
Porque isto funciona?
Isto torna o getElementById obsoleto?
Posted
Updated 16-Jan-15 5:11am
v2
Comments
DamithSL 16-Jan-15 10:59am    
please ask question in english
OriginalGriff 16-Jan-15 11:06am    
I'm sure English is not your native language, but it is the default language for this site.
Please, either try to find a better translation of your question to English, or find a site in your own native language, as they may be able to help you better than we can!
Use the "Improve question" widget to edit your question and provide better information.

[Google Translate]

Tenho certeza de que o Inglês não é sua língua nativa, mas é a linguagem padrão para este site.
Por favor, seja tentar encontrar uma tradução melhor da sua pergunta para Inglês, ou encontrar um site em sua própria língua nativa, como eles podem ser capazes de ajudá-lo melhor do que nós podemos!
Use o widget "Improve question" para editar a sua pergunta e fornecer melhores informações.

1 solution

These StackOverflow threads have a decent explanation:
Do DOM tree elements with ids become global variables?[^]
Is there a spec that the id of elements should be made global variable?[^]
Why don't we just use element IDs as identifiers in JavaScript?[^]

Basically, Internet Explorer introduced this non-standard behaviour, and other browsers have copied it for compatibility. This is now a documented part of the HTML5 specification: Named access on the Window object[^]

Google Translate

Estes tópicos StackOverflow têm uma explicação decente:
fazer elementos de árvore DOM com ids tornam-se variáveis globais?[^]
Existe uma especificação que o id de elementos devem ser feitas variável global[^]
Por que nós não apenas usar IDs de elementos como identificadores em JavaScript[^]

Basicamente, o Internet Explorer apresentou este comportamento não-padrão, e outros navegadores copiou-lo para compatibilidade. Esta é agora uma parte documentada da especificação HTML5: Named acesso no objeto Window[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900