How to download the current page as a file / attachment using Javascript?

I am aware of the hidden iFrame trick as mentioned here ( and in other answers.

I am interested in a similar problem:

How can I use Javascript to download the current page (IE: the current DOM, or some sub-set of it) as a file?

I have a web page which fetches results from a non-deterministic query (eg. a random sample) to display to the user. I can already, via a querystring parameter, make the page return a file instead of rendering the page. I can add a "Get file version" button (our standard approach) but the results will be different to those displayed because it is a different run of the query.

Is there any way via Javascript to download the current page as a file, or is copying to the clipboard my only option?

EDIT An option suggested by Stefan Kendall and dj_segfault is to write the result server side for later retrieval. Good idea, but unfortunately writing files server side is out of the question in this instance.

How about shudder passing the innerHTML as a post parameter to another page?


  • You can try with the protocol data:text/attachment

    Like in:

        <div id="hello">
        document.location = 
            'data:text/attachment;,' + //here is the trick
                //document.documentElement.innerHTML; //To Download Entire Html Source

    Edit after shesek comment