If you want the image to load and display a specific image, use .src to load that image URL.
If you need some of the metadata (for any tag) that may contain a URL, use data-src or whatever data-xxx you want to select.
MDN documentation on data-xxxx attributes: https://developer.mozilla.org/en-US/docs/DOM/element.dataset
An example of src in an image tag, where the image loads a JPEG for you and displays it:
<img id="myImage" src="http://mydomain.com/foo.jpg"> <script> var imageUrl = document.getElementById("myImage").src; </script>
The example "data-src" on a tag without an image where the image has not yet been loaded is just a fragment of the metadata in the div tag:
<div id="myDiv" data-src="http://mydomain.com/foo.jpg"> <script> // in all browsers var imageUrl = document.getElementById("myDiv").getAttribute("data-src"); // or in modern browsers var imageUrl = document.getElementById("myDiv").dataset.src; </script>
An example of data-src in an image tag used as a place to store the alternate image URL:
<img id="myImage" src="http://mydomain.com/foo.jpg" data-src="http://mydomain.com/foo.jpg"> <script> var item = document.getElementById("myImage"); </script>
jfriend00 Mar 10 '13 at 7:56 2013-03-10 07:56
source share