Save HTML locally with Javascript

后端 未结 10 1434
夕颜
夕颜 2020-12-03 01:19

I do know that Javascript cannot write data in the filesystem, for security reasons. I have often read that the only way to save data locally with Javascript is cookies or <

相关标签:
10条回答
  • 2020-12-03 02:09

    Just use https://github.com/firebase/firepad — See it in action
    This doesn’t need a server on your computer, it will reach out and save the data remotely.

    0 讨论(0)
  • 2020-12-03 02:10

    Yes, it is possible. Proof by example:

    TiddlyFox: allows modification of local files via an add-on. (source code) (extension page):

    TiddlyFox is an extension for Mozilla Firefox that enables TiddlyWiki to save changes directly to the file system.

    Todo.html: An HTML file that saves edits to itself. Currently, it only works in Internet Explorer and you have to confirm some security dialogs when first opening the file. (source code) (functional demo).

    Steps to confirm todo.html actually saves changes to itself locally:

    1. Save todo.html to local harddrive
    2. Open with Internet Explorer. Accept all the security dialogs.
    3. Type command todo add TEST (todo.html emulates the command-line interface of todo.txt-CLI)
    4. Inspect todo.html file for addition of 'TEST'

    Caveats: there is no cross-platform method. I'm not sure how much longer these methods will exist. When I first started my todo.html project, there was a jQuery plugin called twFile that allowed cross-browser loading/saving of local files using four different methods (ActiveX, Mozilla XUL, Java applet, Java Live Connect). Except for ActiveX, browsers have disallowed all these methods due to security concerns.

    0 讨论(0)
  • 2020-12-03 02:12

    Convert your HTML content to a data uri string, and set as href attribute of an anchor element. Don't forget to specify a filename as download attribute.

    Here's a simple example:

    <a>click to download</a>
    <script>
        var anchor = document.querySelector('a');
        anchor.setAttribute('download', 'example.html');
        anchor.setAttribute('href', 'data:text/html;charset=UTF-8,<p>asdf</p>');
    </script>
    

    Just try it in your browser, no server required.

    0 讨论(0)
  • 2020-12-03 02:13

    Have a look into this :) Download File Using Javascript/jQuery there should be everything you need. If you still need help or it's not the solution you need, tell me ;)

    0 讨论(0)
提交回复
热议问题