Source srcAttribute

Source 对象参考手册Source Object

Example

Return the URL of the media file:

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

xThe output result is:

https://www.example.com/try/demo_source/horse.mp3

Try it yourself »

Definition and Usage

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

The src attribute specifies the URL of the media file to be played.

Note:The <source> element is new in HTML5.

Note:Internet Explorer 8 and earlier versions only return the file name (e.g., horse.mp3) rather than the URL (e.g., http://www.w3cschool.cc/jsref/horse.mp3).


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the src attribute.


Syntax

Return the src attribute:

sourceObject.src

Set the src attribute:

sourceObject.src=URL

Attribute Values

Value Description
URL Specifies the URL of the media file.

Possible values:

  • Absolute URL - points to another website (e.g., href="http://www.example.com/horse.mp3")
  • Relative URL - points to a file within a website (e.g., href="horse.mp3.html")

Technical Details

Return value: A string representing the URL of the media file. Returns the full URL, including the protocol (e.g., http://).


Related Articles

HTML Reference Manual:HTML <source> src Attribute


Source 对象参考手册Source Object

Other Extensions