Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8306065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:15:20+00:00 2026-06-08T18:15:20+00:00

I have an HTML file that is using Javascript to do file I/O operations

  • 0

I have an HTML file that is using Javascript to do file I/O operations on a .txt file, via an ActiveXObject (only works in Internet Explorer, on Windows OS).

There is a text input box on the HTML page, and a button. The button calls a function  onclick  to write the text entered to the end of the .txt file. There is also a textarea on the HTML page, in which the modified contents of the .txt file are copied and pasted into. All of this is working so far…


So, I want to insert tabs and new-lines into the .txt file, from my HTML page with Javascript. I am using this line to copy the .txt file contents into the textarea, initialized in a variable:

var newText = oldText + "\n" + document.getElementById("userInput").value;

Of course, the escape character  \n  works on the HTML page, and not in the .txt file…


So how do I encode new lines, and tabs as well, into a parsable format for the .txt file? I have tried using the  escape()  method on  ANSI  values found here, and on  ASCII  values found here, but with no luck.

Here is my code so far:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>New Web Project</title>
    </head>
    <body>

        <p>
            Enter some text here: &nbsp;
            <input type = "text" id = "userInput" />
        </p>

        <input type = "button" value = "submit" onclick = "main();" />
        <br />
        <hr />
        <br /><br /><br />
        <textarea id = "textHere" rows = 25 cols = 150></textarea>

        <script type = "text/javascript">

            // executes all code from this function to prevent global variables
            function main()
            {
                var filePath = getThisFilePath();

                var fileText = readFile(filePath);
                writeFile(filePath, fileText);

            } // end of function main

            function getThisFilePath()
            {
                var path = document.location.pathname;

                // getting rid of the first forward-slash, and ending at the last forward-slash to get rid of file-name
                var correctPath = path.substr(1, path.lastIndexOf("/") );

                var fixedPath = correctPath.replace(/%20/gi, " "); // replacing all space entities

                return fixedPath;
            } // end of function getThisFilePath

            function readFile(folder)
            {
                var fso = "";
                var ots = "";
                var oldText = "";

                try
                {
                    fso = new ActiveXObject("Scripting.FileSystemObject");

                    // in the same folder as this HTML file, in "read" mode (1)
                    ots = fso.OpenTextFile(folder + "writeToText.txt", 1, true);

                    oldText = ots.ReadAll();

                    ots = null;
                    fso = null;
                }
                catch(e)
                {
                    alert("There is an error in this code!\n\tError:  " + e.message);
                    exit(); // end the program if there is an error
                }

                return oldText;

            } // end of function readFile

            function writeFile(folder, oldText)
            {
                var fso = "";
                var ots = "";

                var newText = oldText + "\n" + document.getElementById("userInput").value;

                try
                {
                    fso = new ActiveXObject("Scripting.FileSystemObject");

                    // in the same folder as this HTML file, in "write" mode (2)
                    ots = fso.OpenTextFile(folder + "writeToText.txt", 2, true);

                    ots.Write(newText);
                    ots.Close();

                    ots = null;
                    fso = null;
                }
                catch(e)
                {
                    alert("There is an error in this code!\n\tError:  " + e.message);
                    exit(); // end the program if there is an error
                }

                setText(newText); // with the function below

            } // end of function writeFile

                // called from the function writeFile
                function setText(textFile)
                {
                    document.getElementById("textHere").value = textFile;

                } // end of function setText

        </script> <!-- end of javascript -->
    </body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:15:21+00:00Added an answer on June 8, 2026 at 6:15 pm

    Windows expects "\r\n" as linebreaks. I’m quite sure you would find them in your textarea’s value as well (after hitting enter). They will get automatically inserted when you set a value with "\n", and most libraries (like jQuery) do replace them with “normal” linebreaks when reading the value.

    However, I would expect a file read/write with only "\n" to work, and when you load the file’s text into your textarea they should show up. MS Notepad might have problems showing them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML (App) file that reads another HTML (data) file via jQuery.ajax()
I have a HTML file that contains only the basic elements (html, head, body,
Say, I've got an HTML file that I want to process using Javascript. For
I have a simple html file. Using javascript I am opening a https link
I have a html file that has invoice details I would like to know
I have a html file that contains urls that start with ftps, http, https,
I have a html file that I want to trim. I want to remove
I'm new to DOM parsing in PHP: I have a HTML file that I'm
I have a single .html file that looks similar to the following: <div id=myPage
I have a HTML/CSS File laid out with a bunch of areas that need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.