Gunnar Bittersmann: img.naturalWidth

Beitrag lesen

@@wonk

<img src="meinBild.jpg" name= "Bild">
<script  type="text/javascript" >
Bildobjekt = document.getElementsByName("Bild");
alert(Bildobjekt.naturalWidth);
</script>

Ich wusste gar nicht, dass ein img-Element ein name-Attribut haben kann. Mal in die Spec geschaut: Kann’s auch gar nicht. Der Validator meint dazu: Error: The name attribute on the img element is obsolete.

Ah, das gab’s irgendwann mal; deshalb geht auch getElementsByName() hier (noch). Da Browser i.a.R. abwärtskompatibel sein wollen, würde ich nicht erwarten, dass sie das bald nicht mehr unterstützen, aber verlassen würde ich mich auch nicht darauf.

Der Validator weiter: Use the id attribute instead. Genau das hätte ich auch gesagt. Und getElementById() hat kein s drin. (querySelector() auch nicht – sondern ein S. 😉)

🖖 Live long and prosper

--
“In my home, the America I love, the America I've written about, that has been a beacon of hope and liberty for 250 years, is currently in the hands of a corrupt, incompetent and treasonous administration. Tonight, we ask all who believe in democracy and the best of our American spirit, to rise with us, raise your voices against authoritarianism, and let freedom reign.”
— Bruce Springsteen, Manchester 2025-05-14