Paul,
To a certain way of thinking, what you're asking is simply not possible using the parameters you describe. JavaScript has no capability of writing a file, much less HTML.
However, from a different point of view, this is do-able.
Consider a browser. It allows you to open files, read files, save files. A browser can open a text file, allow you to change it and save it back to your drive. When viewed in a certain light, a browser can be a basic
file-handling object, much like I (and a lot of others) used to use WordPerfect's file handling features to manipulate files before Windows was terribly useful (attention Microsoft: on occasion, someone makes a better interface than you and on occasion, they're in Orem, Utah).
With
that in mind, it's a no-brainer to realize that an HTML file is a text file and the browser is your manipulative tool. Well, JavaScript can certainly open an HTML file, as well as a plain-text file.
Behold:
Copy this and save it as "Sample.html":
Code:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></meta>
<title>JavaScript Sample</title>
<link href="style.css" type="text/css" rel="stylesheet"></link>
<script src="FileOps.js" type="text/javascript"></script>
</head>
<body>
<form name="MainForm">
<p><input type="button" value="Make My HTML File!" onclick="HTMLFileCreate();return true;"></input></p>
<p><input type="button" value="Make My Text File!" onclick="TextFileCreate();return true;"></input></p>
</form>
</body>
</html>
and copy this and save it as FileOps.js:
Code:
function HTMLFileCreate()
{
var w = window.open();
var d = w.document;
d.writeln('<!DOCTYPE html ');
d.writeln(' PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"');
d.writeln(' "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');[/URL]
d.writeln('');
d.writeln(' <html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">');
d.writeln(' <head>');
d.writeln(' <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></meta>');
d.writeln(' <title>My Document</title>');
d.writeln(' </head>');
d.writeln(' <body>');
d.writeln(' <h2>Title</h2>');
d.writeln(' <p>The body of the document.</p>');
d.writeln(' </body>');
d.writeln('</html>');
d.close();
}
function TextFileCreate()
{
var w = window.open();
var d = w.document;
d.writeln('Just a text file');
d.close();
}
As you can see, it's totally possible -- once you accept using the browser as your file object.
Cheers,
![[monkey] [monkey] [monkey]](/data/assets/smilies/monkey.gif)
Edward
"Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!" -- inventor of the cat door