Download File Using Javascript/jQuery

前端 未结 28 3471
悲&欢浪女
悲&欢浪女 2020-11-21 05:11

I have a very similar requirement specified here.

I need to have the user\'s browser start a download manually when $(\'a#someID\').click();

But

28条回答
  •  日久生厌
    2020-11-21 05:38

    If you are already using jQuery, you could take adventage of it to produce a smaller snippet
    A jQuery version of Andrew's answer:

    var $idown;  // Keep it outside of the function, so it's initialized once.
    downloadURL : function(url) {
      if ($idown) {
        $idown.attr('src',url);
      } else {
        $idown = $('