简体   繁体   中英

Multiple Line textfile output html

I am trying to output the lines of a textfile to a div in an HTA. The text itself comes up just fine, however the lines do not carry over. Instead, the text is grouped together on one big line. If I print to a msgbox it comes up with correct, separated, lines.

function updateTPtally()
{
    var fso, appdir, messagefile, ts, messagetext, line;
    fso = new ActiveXObject("Scripting.FileSystemObject");

    if (MESSAGE_FILE_NAME7.indexOf("\\") == -1) {
        appdir = unescape(fso.GetParentFolderName(location.pathname));
        messagefile = fso.BuildPath(appdir, MESSAGE_FILE_NAME7);
    } else {
        messagefile = MESSAGE_FILE_NAME7;
    }

    try {
        // Open the message-of-the-day file as a TextStream.
        ts = fso.OpenTextFile(messagefile, 1);
        messagetext = "";
        while (! ts.AtEndOfStream) {
            line = ts.ReadLine();
            // If the line contains text, enclose in <p> element;
            // otherwise, construct a blank line with a nonbreaking space.
            if (line.length > 0)
                line = line.replace(/^(.*)$/, "$1");
            else
                line = "<p>&nbsp;</p>";
            messagetext += line;
        }
        ts.Close();
    }

    // Construct an error message if an error occurred.
    catch(err) {
        messagetext = "<p>Can't display the message of the day.</p>"
        + "<p>&nbsp;</p>"
        + "<p>Error <b>0x" + hex(err.number) + "</b><br />"
        + err.description + "</p>";
    }

    // Update the innerHTML element of the textarea element with the
    document.getElementById("TPtallymemo").innerHTML = messagetext;
}

EDIT: I have added line = line.replace(/\\n/g, "
");

This seems to work, however the first word of the text. This is my textfile:

Hello.

This should be line two.  And written all in one line.

This should be line three, and also written on one line.

This is what prints out in my span:

Hello.


This
should be line two. And written all in one line.


This
should be line three, and also written on one line.

You are not enclosing the lines on the text after you replace them. Also, you shouldn't enclose the non-breaking space, as paragraph elements account to be correctly separated from each other.

Just one small final observation: you should call for AtEndOfStream() with the parentheses in your while condition.

messagetext = "";
while (! ts.AtEndOfStream()) {
    line = ts.ReadLine();
    // If the line contains text, enclose in <p> element;
    // otherwise, construct a blank line with a nonbreaking space.
    if (line.length > 0)
        line = line.replace(/^(.*)$/, "<p>$1</p>");
    messagetext += line;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM