Don't Panic
Don't Panic

Reputation: 41810

Why will newlines from my javascript prompt no longer paste into windows applications?

This function gets the text from a one row of a table and displays it, along with the text from the header row in a prompt, so that a user can copy and paste from the prompt. It works, but for some reason the newlines are no longer included when copying/pasting from the prompt. I have not changed this at all, and it was working just fine this morning, unless I was hallucinating.

$(".display-row").on('click', function () {
    var tr = $(this).closest('tr'),
        headers = $("#index-table-headers").children(),
        displayText = "";
    tr.children().each(function (i) {
        var headerText = $(headers.get(i)).text(),
        valueText = $(this).text();
        if (headerText.trim()) { // prevents this button from being included
            displayText += headerText + ": " + valueText + "\n";
        }
    });

    window.prompt('This can be used to copy and paste the record', displayText);
});

I have seen in multiple other Q&As that appending a newline to a string should allow it to be included in a prompt; I know it can work, because it DID work earlier. Is there anything in this code that would make it not work, or should I try to look for some other reason?

EDIT: I am in Windows, so I tried using \r\n as Justin Powell recommended. I am sure the \r\n is in the displayText (I can see it in firebug right before opening the prompt.) In fact, in firebug the variable actually expands to multiple lines. Unfortunately, no matter which application I try to paste it into, I still do not get them.

Upvotes: 1

Views: 941

Answers (2)

Don't Panic
Don't Panic

Reputation: 41810

I figured this out. It was apparently not working because I was testing it with Firefox. When I use IE or Chrome, it works as I intended it to. Chrome actually shows box characters in the prompt to represent the unprintable characters. IE doesn't, but apparently they are still there, because there are multiple lines when I paste the text into other applications. I also discovered that it does not seem to matter whether I use \n or \r\n in the Javascript. It works either way in Chrome and IE, and neither way in Firefox. Maybe Chrome and IE convert the line endings to Windows style before displaying the string in the prompt; I'm not sure, but it works for whatever reason. I am going to keep using the same Javascript for now. It is a temporary fix for a small group of users who are restricted to using internet explorer, so it should be ok for my purposes. But using a prompt is really not a good way to do this type of thing, and I would not do it this way for any permanent solution.

Upvotes: 2

JstnPwll
JstnPwll

Reputation: 8685

The linefeed character is there, but you can't see it (depending on where you paste).

In Windows, a new line actually consists of a carriage return + linefeed, or \r\n. Try using this instead of simply \n.

Upvotes: 2

Related Questions