1

I want to download a HTML file on click of a span.

I am not using any anchor tag in this. I want to do this on simple click of a span.

Url is having .html extension. And the url to html file is on another domain(Amazon s3)

How to achieve this in JavaScript, as in anchor tag it would have been easy where I would have written attribute 'download' in it.

5
  • please put your code here. Commented Jan 22, 2015 at 10:56
  • 1
    jQuery is fine? or pure javaScript Commented Jan 22, 2015 at 10:58
  • Jquery will work fine, however I don't want to use any other jquery plugin Commented Jan 22, 2015 at 11:04
  • from the same domain or cross domain ? Commented Jan 22, 2015 at 11:07
  • The url to .html file is on another machine, i.e. it saved on s3 amazon Commented Jan 22, 2015 at 11:29

4 Answers 4

2

This is actually a matter of setting the current location of the page to data:text/attachment in Firefox. In Chrome, it seems like setting the location won't trigger a file download. Below is my proposition that lets you specify the filename and also the part of the website you want to download as HTML (with indentation preserved).

function toBase64 (str) { return window.btoa(unescape(encodeURIComponent(str))); } function triggerDownload () { var html = 'data:text/attachment;base64,' + toBase64(document.querySelector('html').innerHTML); var evt = new MouseEvent('click', { view: window, bubbles: false, cancelable: true }); var a = document.createElement('a'); a.setAttribute('download', 'file.html'); a.setAttribute('href', html); a.setAttribute('target', '_blank'); a.dispatchEvent(evt); } document.querySelector('span').addEventListener('click', function () { triggerDownload(); });
span { color: green; cursor: pointer; }
<h1>Click <span>here</span> to download</h1>

From How to download the current page as a file / attachment using Javascript? you can see other examples, where it is also possible to download a determined part of the page, etc. (ps: the snippet won't run from within the iframe that Stack Overflow adds).

Sign up to request clarification or add additional context in comments.

5 Comments

Using this code open a page(in same tab) with url like below data:text/attachment;base64,aHR0cDovL3BheXRtcHVibGljLnMzLWwLXNvdXRoZWFzdC0xLmFtYXpvbmF3cy5jb20vbWVyY2hhbnRfdG5jLzQvbWVyY2hhbnRfNF8xLjIzXzE0MjE5MTk1Mzk3MjJfc2lnbmVkLmh0bWw= and only url as text is displayed in the screen
hmm, that's true. It's working fine for FF (37) but not for Chrome (at least here in chrome 41). I'll check it
just updated following muaz-khan.blogspot.in/2012/10/… but with the possibility of grabbing any part of the website
methods mentioned in the blogs helps in achieving my requirement
That's great! Also, regarding document.createElement and GC (for curiosity): stackoverflow.com/questions/1847220/… (i was wondering about if we could create a mess with a bunch of HTMLElements being created)
0

Thanks for the answers and comments.

I am now able to download the file, however by violating the prerequisite of the question(i.e. anchor tag usage)

I followed this. The methods below in the link does the job

function SaveToDisk(fileUrl, fileName) { var hyperlink = document.createElement('a'); hyperlink.href = fileUrl; hyperlink.target = '_blank'; hyperlink.download = fileName || fileUrl; var mouseEvent = new MouseEvent('click', { view: window, bubbles: true, cancelable: true }); hyperlink.dispatchEvent(mouseEvent); (window.URL || window.webkitURL).revokeObjectURL(hyperlink.href); } 

However I now need to destroy the anchor tag created after downloading the link.

Comments

0

You are trying to make a cross domain request. In order to do that you will have to enable cross-origin resource sharing (CORS). Fortunately S3 supports the CORS request. You need to enable it by adding the CORS configuration.

See the AWS article on How Do I Enable CORS on My Bucket?

Download file with AJAX in browser

function download() { $.ajax({ url: "https://s3.amazonaws.com/123d_test/yourHtmlFile.html", type: "GET", dataType: "text", success: function (data, textStatus,jqXHR) { alert(data.toString()); console.log(data);}, error: function (data, textStatus, jqXHR) {alert("error"); console.log(textStatus);} }); } 

Source

Comments

-1

If you are using HTML5 you can use the download attribute:

<a href="something.html" download="something.html">Download</a> 

1 Comment

I don't want to use anchor tag

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.