Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I had a similar situation (but wasn't willing to give up altogether). I'm constructing a save-friendly version of a webpage using Javascript that I want the user to download as a text file (comma-separated values, in my case). I think <code>data:</code> URIs can help here.</p> <pre><code>//construct the csvOutput in Javascript first var popup = window.open("data:application/octet-stream," + encodeURIComponent(csvOutput), "child"); //no need to document.write() anything in the child window </code></pre> <p>In Firefox, this doesn't pop up a window even, just asks the user if they want to save the file, and saves it as a .part file. Not exactly ideal, but at least it saves the file without popping up an unnecessary window.</p> <p>Alternatively, we can use the text/plain MIME type:</p> <pre><code>//construct the csvOutput in Javascript first var popup = window.open("data:text/plain;charset=utf-8," + encodeURIComponent(csvOutput), "child"); </code></pre> <p>In Firefox, this does pop open a new window, but then it's saved by default as ASCII text, without any of the cruft of the parent window or any line-wrapping. This is probably what I will use.</p> <p>It looks like this won't work in IE though. IE 8 is the only version that supports <code>data:</code> URIs, and it has a <a href="http://msdn.microsoft.com/en-us/library/cc848897%28VS.85%29.aspx" rel="noreferrer">series of restrictions</a> on where it can be used. For IE, you might look at <a href="http://msdn.microsoft.com/en-us/library/ms536419(VS.85).aspx" rel="noreferrer">execCommand</a>.</p> <p>Thanks to this <a href="http://www.tek-tips.com/viewthread.cfm?qid=1517391&amp;page=1" rel="noreferrer">tek-tip thread</a> and the <a href="http://en.wikipedia.org/wiki/Data_URI_scheme" rel="noreferrer">Wikipedia article on the data URI scheme</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload