Downloading a variable

javascript: URIs should work for this - indeed, this is exactly what they're meant for. However, IE doesn't honour the type attribute, and in Safari this technique has no effect at all.

data: URIs work in Firefox (3.0.11) and Safari (4.0) (and probably other compliant browsers), but I can't get this approach to work in IE (8.0). (All tested in Windows)

<a href="data:text/plain,The%20quick%20brown%20fox%20jumps%20over%20the%20lazy%20dog.">Data URI</a>

This isn't a JS solution in itself, but JS can be used to set the href dynamically. Use the escape function to turn raw text/data into URI-encoded form.

Combining this with detecting IE and using the IE-specific solution already linked to might do what you want....

I shall add that you can't force it to trigger a download dialog (that's beyond the scope of both HTML and JS), but you can persuade it to do so by setting application/octet-stream as the type. Trouble is that the user'll then have to add the right filename extension manually.


See the accepted answer to my question here. This is only possible in IE browsers.

document.execCommand('SaveAs',true,'file.xml')

A possible option would be to use JavaScript to generate a link with a href using the data: URL scheme, tho' this might require some fancy coding to pull off properly.

Tags:

Javascript