Javascript Reference - HTML DOM Anchor download Property








The download attribute, new for the <a> element in HTML5, sets that the target will be downloaded when a user clicks on the hyperlink.

The download property sets or gets the value of the download attribute of a link.

Browser Support

download Yes No Yes No Yes

Syntax

Return the download property:

anchorObject.download 

Set the download property:

var v = anchorObject.download=filename;




Property Values

Value Description
filename Set filename for the download. If omitted, the original filename is used.

Return Value

A String representing the name of the downloaded file.

Example

The following code shows how to get the value of the download attribute of a link.


<!DOCTYPE html>
<html>
<body>
<a id="myAnchor" href="http://java2s.com/style/demo/border.png" download="an image">
<img border="0" src="http://java2s.com/style/demo/border.png" alt="an image" width="104" height="142"></a>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<!--from   w w w .ja  v  a 2  s  .c  om-->
<script>
function myFunction() {
    var x = document.getElementById("myAnchor").download;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to set the value of the download attribute of a link.


<!DOCTYPE html>
<html>
<body>
<!--  w  w w. j ava  2 s. c  om-->
<a id="myAnchor" href="http://java2s.com/style/demo/border.png" download="an image">
<img border="0" src="http://java2s.com/style/demo/border.png" alt="an image" width="104" height="142"></a>
<button onclick="myFunction()">test</button>

<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("myAnchor").download = "newValue";
    document.getElementById("demo").innerHTML = "changed";
}
</script>

</body>
</html>

The code above is rendered as follows: