0

I need to save a URL in an anchor tag on a page when a button is clicked. How can I do this with jQuery or pure JS?

If possible, can it "Save As" or otherwise name the file being saved from the URL a string that is on the page (from a specific heading tag with the id "heading", for instance)?

EDIT

To clarify:

<button id="button">Button</button>

<h1 id="heading">Website Title</h1>

<a href="http://website.com/sample.zip" id="file">File</a>

...how do I make it so that when the button is clicked it will automatically start downloading the ZIP file from the URL in the anchor tag?

Moreover, if possible, how do I have the file save with the name of the H1 tag (i.e. "Website Title.zip")?

Simon Adcock
  • 3,554
  • 3
  • 25
  • 41
Charles
  • 167
  • 1
  • 5
  • 15

1 Answers1

1

If it is actually a .zip or a file with an extension that wouldn't normally be viewed in the browser, you can do it like this:

HTML:

<button id="button">Button</button>

<h1 id="heading">Website Title</h1>

<a href="http://website.com/sample.zip" id="file">File</a>

jQuery:

$("#button").click(function(){
    window.location.assign($("#file").attr("href")); 
});

This will prompt you to download the file (in chrome it usually automatically starts the download) assuming that it can't be viewed by the browser. Not sure about the "save-as" part.

Nile
  • 1,586
  • 1
  • 14
  • 25