Reputation: 53
The following HTML file when opened in chrome, downloads the test.gif file. On firefox/IE, nothing happens.
<html>
<head>
<script type="text/JavaScript" language="JavaScript">
function s() {
var link = document.createElement('a');
link.download = "test.gif";
link.href = 'http://192.168.20.22/mantis/images/mantis_logo.gif';
link.click();
}
</script>
</head>
<body onload="s()" >
</body>
</html>
Firebug tells me that the link object is properly created and href is set, but somehow on calling link.click() nothing happens. Any idea why?
Upvotes: 2
Views: 517
Reputation: 9691
I think it needs to be added to the DOM before it will work. Try this:
function s() {
var link = document.createElement('a');
link.download = "test.gif";
link.href = 'http://192.168.20.22/mantis/images/mantis_logo.gif';
document.body.appendChild(link);
link.click();
}
And if you don't want it hanging around you can immediately remove it as well by adding this after the click:
link.parentNode.removeChild(link);
Upvotes: 2