Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving a string to a file

Status
Not open for further replies.

Packermann

Programmer
May 17, 2000
15
US
I need to save a string value to a file. The File Save dialog must first come up so that the user can select where he wants to save it. This needs to be written in Javascript or HTML. Could someone please help me?


Paul
packermann@niku.com

 
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 &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
     &quot;[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>[/URL]

<html xmlns=&quot;[URL unfurl="true"]http://www.w3.org/1999/xhtml&quot;[/URL] xml:lang=&quot;en&quot; lang=&quot;en&quot;>
  <head>
    <meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;></meta>
    <title>JavaScript Sample</title>
    <link href=&quot;style.css&quot; type=&quot;text/css&quot; rel=&quot;stylesheet&quot;></link>
    <script src=&quot;FileOps.js&quot; type=&quot;text/javascript&quot;></script>
  </head>
  <body>
    <form name=&quot;MainForm&quot;>
      <p><input type=&quot;button&quot; value=&quot;Make My HTML File!&quot; onclick=&quot;HTMLFileCreate();return true;&quot;></input></p>
      <p><input type=&quot;button&quot; value=&quot;Make My Text File!&quot; onclick=&quot;TextFileCreate();return true;&quot;></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 &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;');
    d.writeln('     &quot;[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>');[/URL]
    d.writeln('');
    d.writeln('  <html xmlns=&quot;[URL unfurl="true"]http://www.w3.org/1999/xhtml&quot;[/URL] xml:lang=&quot;en&quot; lang=&quot;en&quot;>');
    d.writeln('  <head>');
    d.writeln('    <meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;></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] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
This should do it:

<script>
function saveString(myString){
var str=window.open()
str.document.write(myString)
str.document.execCommand(&quot;saveas&quot;)
str.close()
}
saveString(&quot;test&quot;)
</script>

Adam
while(woman.width>woman.height && wallet.value>0){beer++;vision.blur()};
 
Adam,

What is &quot;execCommand&quot;?

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Ooooh, a Microsoft-only thing.

Paul, do you care if your solution doesn't work in a non-IE environment? If so, this looks like it might work for you.

[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top