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.
download |
Yes | No | Yes | No | Yes |
Return the download property:
anchorObject.download
Set the download property:
var v = anchorObject.download=filename;
Value | Description |
---|---|
filename | Set filename for the download. If omitted, the original filename is used. |
A String representing the name of the downloaded file.
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:
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: