Image src Property

Image Object Reference Image Object

Example

Change the URL of an image:

document.getElementById("myImg").src = "hackanm.gif";
Try it yourself »

Definition and Usage

The src property sets or returns the value of the src attribute of an image.

The required src attribute specifies the URL of an image.

Note: The src property can be changed at any time. However, the new image inherits the height and width attributes of the original image, if not new height and width properties are specified.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The src property is supported in all major browsers.


Syntax

Return the src property:

imageObject.src

Set the src property:

imageObject.src=URL

Property Values

Value Description
URL Specifies the URL of the image.

Possible values:
  • An absolute URL - points to another web site (like src="//www.example.com/default.htm")
  • A relative URL - points to a file within a web site (like src="default.htm")

Technical Details

Return Value: A String, representing the URL of the image. Returns the entire URL, including the protocol (like http://)

More Examples

Example

Return the URL of an image:

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

The result of x will be:

//www.w3schools.com/jsref/compman.gif
Try it yourself »

Related Pages

HTML reference: HTML <img> src attribute


Image Object Reference Image Object