It is not exactly true. Any browser is an application to display HTML
documents. Any other type of document - including text/plain -
requires a separate plugin so to be displayed as separate object
withing HTML page. An exception is made for text/plain as a very
common type displayed in browsers. Instead of plugin, browser at
runtime generates a minimum HTML page
<HTML><BODY><PRE>text/plain content</PRE></BODY></HTML>
and displays it. This mechanics goes on the background, so if you say
saving a page, it will be again some.txt Unfortunately this mechanics
is not fully hidden on Gecko so say documentElement.firstChild returns
HEAD element for text/plain which may be very confusing if not knowing
the background mechanics.
Back to the OP question and as already pointed by Thomas, there is a
convenient solution for IE, much lesser convenient for Gecko - and
maybe newer Gecko has something better to propose and maybe IE7 has
more strict security for execCommand, needs to be checked. I am
talking only about solutions within the default security restrictions.
For IE one can use document.execCommand with SaveAs argument.
For Gecko one can use bug # 367231
https://bugzilla.mozilla.org/show_bug.cgi?id=367231
as it was explained to me by developers it is not a bug but a feature
so to remain in future releases.
So it maybe something like (test.txt file has to be in the same
directory where the page is in order to see the script in action):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Save As</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function saveAs(doc) {
if (typeof document.execCommand == 'object') {
doc.execCommand('SaveAs', false, 'C:\\foobar.txt');
}
else if (
(navigator.userAgent.indexOf('Gecko')!=-1) &&
(netscape) &&
(netscape.security)) {
var txt = doc.getElementsByTagName('PRE')[0].firstChild.nodeValue;
window.location = 'data
lain/text,' + escape(txt);
}
else {
}
}
function init() {
window.frames['UIN'].document.designMode = 'on';
}
window.onload = init;
</script>
</head>
<body>
<form method="post" action="">
<fieldset>
<legend>Save As</legend>
<iframe name="UIN" src="test.txt"
style="
width: 20em;
height: 3em;
display: -moz-inline-box;
display: inline-block;"></iframe>
<br>
<input type="button" value="Save As"
onclick="saveAs(window.frames['UIN'].document)">
</fieldset>
</form>
<noscript>
<p style="color:red">If you see this text then it means
that the current browser settings do not allow script
execution for this page.</p>
</noscript>
</body>
</html>