Javascript Reference - HTML DOM Source src Property








The src property sets or gets the value of the src attribute in a <source> element.

Browser Support

src Yes Yes Yes Yes Yes

Syntax

Return the src property.

var v = sourceObject.src 

Set the src property.

sourceObject.src=URL

Property Values

Value Description
URL Specifies the URL of the media file.

Possible values.





Return Value

A String type value representing the URL of the media file.

Example

The following code shows how to get the URL of a media file.


<!DOCTYPE html>
<html>
<body>
<!-- w w w. ja va 2 s .  c  o m-->
<audio controls>
  <source id="mySource" src="http://java2s.com/style/demo/rain.mp3" type="audio/mpeg">
  <source src="horse.ogg" type="audio/ogg">
</audio>
<p id="demo"></p>

<button onclick="myFunction()">test</button>

<script>
function myFunction() {
    var x = document.getElementById("mySource").src;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: