title: Download file with javascript
body: <p>I want to trigger a file download using javascript only</p>
  
  <p>I know how to do it with html, but i need to trigger the download specifically with javascript, on a button click.</p>
  
  <p>The reason i am not doing it with html is because i want to use a variable as file name later on.</p>
  
  <p>i have used this</p>
  
  <pre><code>$("#button").click(function(){
      window.location = 'files/aaa.pdf';
  });
  </code></pre>
  
  <p>but it only opens the pdf on the same window instead of prompting the user to download it.</p>
  
  <p>I have also tried using this:</p>
  
  <pre><code>&lt;iframe id="my_iframe" style="display:none;"&gt;&lt;/iframe&gt;
  &lt;script&gt;
  function Download(url) {
      document.getElementById('my_iframe').src = 'files/aa.pdf';
  };
  &lt;/script&gt;
  </code></pre>
  
  <p>But it fails to produce results.</p>