1

I was wondering if JavaScript has the ability to write to the file that is open in the browser. I'm developing a page of links to myself to some key websites that I use (for fun, not necessity). Currently I'm trying to develop something where the user can type a link and a name in textboxes and have the link be saved into the HTML file itself. It is obviously easy to add the link and the name to the page with JavaScript, but clearly once the session expires the link will not be added when the page is accessed again. I want to do it this way because I don't want to have to find and load the file every time I want to add a new link. This would be very easy to accomplish using PHP and MySQL, but this file is not running on a server.

So, in summary, is it possible to save the file once it has been modified by the current session?

My code is below:

<!DOCTYPE html>
<html>
<head>
    <script type = "text/javascript">
        function add(link,name)
        {
            htmlstring = "<a href='"+link+"'>"+name+"</a>";
            document.getElementById('urlsdiv').innerHTML += htmlstring;
        }
    </script>
</head>
<body>
    <input type = "button" onclick = "validate(document.getElementById('validbox').value)" value = "check">
    <div id = "urlsdiv">
        <input type = "text" id = "addbox">//Box to type new URL
        <input type = "text" id = "addname">//Box to type link name
        <input type = "button" onclick = "add(document.getElementById('addbox').value,document.getElementById('addname').value)" value = "Add">//Add new link to page
        <a href = "https://www.facebook.com">Facebook</a>//These three links
        <a href = "http://www.youtube.com">YouTube</a>//have already been hard-coded
        <a href = "https://mail.google.com">Gmail</a>//into the file
    </div>
</body>
</html> 
10
  • @aoeu as far as I am aware (through Google), JavaScript file I/O is possible with HTML5 Commented Feb 17, 2014 at 21:07
  • You can't update the HTML file from JavaScript. Any file IO is just in a sandbox. What you can do is save the users values in the browser, using LocalStorage, for example. Commented Feb 17, 2014 at 21:08
  • Use localstorage. Commented Feb 17, 2014 at 21:09
  • @RocketHazmat Thanks for the help. As an alternative, would it be possible to write all the HTML code to a new file, then delete the old file? Commented Feb 17, 2014 at 21:09
  • No. HTML file IO doesn't have access to the real file system. It just can create a sandboxed file system that you can use. See html5rocks.com/en/tutorials/file/filesystem. A quote from that page: With the FileSystem API, a web app can create, read, navigate, and write to a sandboxed section of the user's local file system. Commented Feb 17, 2014 at 21:10

1 Answer 1

2

JavaScript in the browser is in a security sandbox, and has no access to the filesystem, outside of contained areas that the HTML5 File API gives it access to. Currently, the support is very limited, even for those APIs.

Note that I made the distinction of JavaScript inside the browser, because of newer implementations of native JavaScript, such as NodeJS.

Sign up to request clarification or add additional context in comments.

1 Comment

Appreciate the help. Thanks very much :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.