Javascript - Difference between img.src="path" and img.setAttribute('src','path')

Asked

Viewed 675 times

1

What’s the difference between setting the path of an image in Javascript using a variable :

img.src="caminho"

And setting the src attribute, for example:

img.setAttribute('src','caminho')

1 answer

2

For attributes recognized by the tag something as examples:

  • Element.src for tags <img>, <video>, <audio>, <iframe>
  • Element.href for tags <a>, <link>

recognise as an attribute, but in a different element whose attribute src or href are not themselves in the specific browser, or in Customelements (if not implemented correctly) set .src or .href (or another that is not valid, what even following the W3 standards may vary in certain browsers) will be set as property and not as an attribute, for example:

var teste = document.getElementById('teste');

teste.src = 'https://stackoverflow.blog/wp-content/themes/stackoverflow-oct-19/assets/svg/dist/logo.svg';

console.log('getAttribute:', teste.getAttribute('src'));
<p id="teste">elemento p</p>

See that getAttribute('src') returned null

Now see when the attribute is existing/valid for a specific element:

var teste = document.getElementById('teste');

teste.src = 'https://stackoverflow.blog/wp-content/themes/stackoverflow-oct-19/assets/svg/dist/logo.svg';

console.log('getAttribute:', teste.getAttribute('src'));
<img src="" id="teste">elemento p</p>

See that getAttribute('src') returned the image address.

That is, to set custom attributes or non-standard attributes the path is setAttribute(), but if the attribute is "valid" in the element you can use what we call shortcuts, such as .src and .href.

For example, change the property of a CSS inline style with setAttribute can be tricky, but with the style property makes it easy enough, in one element:

 <div style="background: #000; color: #fff; font-family: Arial;">

And in CSS do this to change the color to orange:

 Element.style.color = "#fc0";

It will be much simpler than using setAttribute('style', ...)


A little history about Internet Explorer

I know it’s a browser that no one else uses, at least not versions older than IE10, unless it’s limited to some situation, but I want to talk about the behavior that IE had, properties and attributes in IE6, 7 and 8 also behaved equally, I mean, by setting a property for an element it became an attribute, even if it was not a valid property there

See the IE8 emulation test:

internet explorer 8 ou inferior tratava propriedades como atributos

The code used:

<div id="teste">DIV de teste</div>
<script type="text/javascript">
var teste = document.getElementById('teste');
teste.src = "https://stackoverflow.blog/wp-content/themes/stackoverflow-oct-19/assets/svg/dist/logo.svg";
</script>

Already in IE9+ the behavior was "corrected":

IE9+ o comportamento das propriedades foi corrigido

  • So in case I can give a setAttribute in the element and create the attribute I want?

  • @IOOISISTEMAS exactly, you can create non-standard attributes for the specific element, but if you know that the attribute is valid for the "X" element, then you can use the "property" shortcut that will make typing easier, as it will type less :)

  • And the performance of both?

  • 1

    @hugocsl depends and depends, has who will state which property for known attributes will be faster, and it is likely that it will be even, I have done many benchmark tests, having created my own jQuery with own selector and a number of functionality and for this I can tell you, each browser engine has a different behavior but having the same end result, however speed is something that always varies and could even talk about it in the answer, but it was not quite what the author asked [...]

  • @hugocsl [...] I believe that the best way for anyone to use is to notice that valid properties as attributes is easier to type and develop, especially when it comes to the global property style which is supported by any element, it is certainly simpler to use than to use setAttribute and that alone is enough to assess which one is more practical to use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.