0

I heed to save image on click and for this purpose I use download attribute

<a href="http://mysite.ru/userfiles/certificate_2.png" class="download-certificate-link" data-title="certificate.png" download="certificate.png">Download</a>

and also I would like to add script for browsers which don't support HTML5 download attribute

var a = document.createElement('a');
if (typeof a.download == "undefined") {
    $('body').on('click', '.download-certificate-link', function(){
        $(this).attr('href').download;
        return false;
    });
}

but this script doesn't work. What's wrong and how to fix it?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Heidel
  • 3,174
  • 16
  • 52
  • 84
  • 2
    `$(this).attr('href').download;` is supposed to do what? An attribute is a string and you are accessing a property? – epascarello May 31 '16 at 13:26
  • I've tried to repeat something like that http://stackoverflow.com/questions/17527713/force-browser-to-download-image-files-on-click – Heidel May 31 '16 at 13:35

1 Answers1

-1

Try this

$(document).ready(function(){
    $('.download-certificate-link').each(function(){
        var $this = $(this);        
        $this.wrap('<a href="' + $this.attr('src') + '" download />')
    });
});

JsFiddle Example

Govinda Rajbhar
  • 2,926
  • 6
  • 37
  • 62
  • I don't need to add another `a` tag, I've already have one, and I search a solution for browsers which don't support HTML5 `download` attribute. – Heidel May 31 '16 at 13:40
  • You are right it is not supported with safari except of that this is working with all latest browsers. – Govinda Rajbhar May 31 '16 at 13:47