Latest web development tutorials

HTML DOM Source src attribute

Source Object Reference Source Object

Examples

Returns the URL of the media file:

var x = document.getElementById("mySource").src;

x The output is:

http://www.w3cschool.cc/jsref/horse.mp3

try it"

Definition and Usage

src property sets or returns the <source> element in the value of the src attribute.

Src attribute specifies the URL you want to play media files.

Note: <source> element is new in HTML5.

Note: Internet Explorer version 8 and earlier, only returns the file name (for example horse.mp3), instead of returning URL (for example http://www.w3cschool.cc/jsref/horse.mp3).


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the src attribute.


grammar

Back src attribute:

sourceObject .src

Setting the src attribute:

sourceObject .src=URL

Property Value

描述
URL 规定媒体文件的 URL。

可能的值:

  • 绝对 URL - 指向另一个网站(比如 href="http://www.example.com/horse.mp3")
  • 相对 URL - 指向网站内的一个文件(比如 href="horse.mp3")

technical details

return value: String representing the URL of the media file. Returns the complete URL, including the protocol (such as http: //).


related articles

HTML Reference Manual: HTML <Source> the src attribute


Source Object Reference Source Object