最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Local JavaScript - write to local file - Stack Overflow

programmeradmin2浏览0评论

I have some JavaScript code loaded from a local HTML file (without going through a webserver).. i.e., opened using file://

Is there a way the JavaScript code in this file can be used to write to a local file?

I know that cross-site restrictions do not allow lot of things in JavaScript, but this is not cross-site, so it should be allowed in theory.

I have some JavaScript code loaded from a local HTML file (without going through a webserver).. i.e., opened using file://

Is there a way the JavaScript code in this file can be used to write to a local file?

I know that cross-site restrictions do not allow lot of things in JavaScript, but this is not cross-site, so it should be allowed in theory.

Share Improve this question edited Aug 24, 2016 at 15:21 Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges asked Mar 18, 2011 at 9:06 Jus12Jus12 18k28 gold badges103 silver badges160 bronze badges 1
  • possible duplicate of Self modifying html-JavaScript file – Joachim Breitner Commented Oct 20, 2013 at 17:07
Add a comment  | 

3 Answers 3

Reset to default 11

There's a jQuery plugin jQuery.twFile that allows you to read and write to a local file.

In case of Internet Explorer you can use ActiveX.

<html>
    <head>
        <script type="text/javaScript">
            function WriteToFile()
            {
               var fso  = new ActiveXObject("Scripting.FileSystemObject");
               var txtFile = fso.CreateTextFile("c:\\TestFile.txt", true);
               txtFile.WriteLine("This is a test");
               txtFile.Close();
            }
        </script>
    </head>

    <body>
        <p>
            <script type="text/javaScript">  WriteToFile(); </script>
        </p>
    </body>
</html>

There's no native API* for File System access in browsers. You need that first!

For example, in Internet Explorer, there's an ActiveX object for it:

var fso = new ActiveXObject("Scripting.FileSystemObject");

...but it requires the user to relax their browser settings. On other browsers, you may be able to use a jar (Java Archive) file.

You could also see what non-browser JavaScript containers offer, e.g. Microsoft HTA files (HTML Application) will support the Windows ActiveX FileSystemObject fine, providing your Virus Checking Software allows HTA files to execute.

发布评论

评论列表(0)

  1. 暂无评论