views:

171

answers:

6

Is it possible to create a html file and save it to a directory using JQuery?

+3  A: 

No, Javascript(client side) can't write to files

You can't if you want to create a file on the client machine. If you want to create a file on the server, the backend code can do that.

Svetlozar Angelov
If you take a look over to http://www.tiddlywiki.org you can see how it's done nevertheless @Svetlozar :)But in case you're referring to scripts which originate from a webserver you have to pass several security-warnings to allow the page to access the local disk.
Kosi2801
@Kosi2801 IIRC, Tiddlywiki stores the data in cookies, not as a file in the filesystem.
David Dorward
@david-dorward no, you'd be wrong. when you access the file from a file:// path it updates that file when you save. You have to click ok on a security dialog the first time, but it works seamlessly after that. No cookies though.
MDCore
+2  A: 

No, you won't be able to do that with JavaScript alone.

JavaScript runs in a client browser and saving files to local disk is a security threat.

From JavaScript Security

JavaScript Security Models

The modern JavaScript security model is based upon Java. In theory, downloaded scripts are run by default in a restricted “sandbox” environment that isolates them from the rest of the operating system. Scripts are permitted access only to data in the current document or closely related documents (generally those from the same site as the current document). No access is granted to the local file system, the memory space of other running programs, or the operating system’s networking layer. Containment of this kind is designed to prevent malfunctioning or malicious scripts from wreaking havoc in the user’s environment. The reality of the situation, however, is that often scripts are not contained as neatly as one would hope. There are numerous ways that a script can exercise power beyond what you might expect, both by design and by accident.

The fundamental premise of browsers’ security models is that there is no reason to trust randomly encountered code such as that found on Web pages, so JavaScript should be executed as if it were hostile. Exceptions are made for certain kinds of code, such as that which comes from a trusted source. Such code is allowed extended capabilities, sometimes with the consent of the user but often without requiring explicit consent. In addition, scripts can gain access to otherwise privileged information in other browser windows when the pages come from related domains.

rahul
It appears that the OP wants to save the file on the client side. A server side language would not help.
Crimson
A: 

Simple answer is no. Javascript runs in a sandbox environment and you cannot save files on user computer. You could save it to the server though by sending it through an AJAX request.

Darin Dimitrov
A: 

you can design html pages using javascript but not specifically using JQuery.

Checkout "TIBCO GENERAL INTERFACE"

Download and open sample project.

TIBCO GI is javascript based IDE to design html pages.

http://developer.tibco.com/gi/product%5Fresources37.jsp?tab=#

this. __curious_geek
A: 

You can create a Document, that may contain a complete HTML file using javascript's document Object.

But you can't save it as HTML file using Javascript.

A: 

You cannot use purely Javascript or Jquery to save to the client machine however you can use Javascript and Flash 10 to save small files 3-4kb to the client machine.....go to this page for an example: http://downloadify.info/ and check out the demo.

Sam