Avatar of strimp101
strimp101

asked on 

Newline or Linebreak for Pasted Text in IE with Javascript

I have a JavasScript script that pastes a snippet of code or text with line breaks (/n/r) to a PRE, DIV, or TEXTAREA tag. The purpose is for the user to copy and paste the content to a text file.

When copying and pasting the snippet in FF, the pasted text properly has line breaks. When pasting the text from IE, all the text shows on one line.

How can I ensure that line breaks are not ignored when copying from IE and pasting in a text file?

See DevShed thread for further discussion.
JavaScriptWeb DevelopmentHTML

Avatar of undefined
Last Comment
coldfusionwebdev
Avatar of HainKurt
HainKurt
Flag of Canada image

i cannot get the issue...

when you browse the code below, and copy paste it to notepad, i see it properly, also when i paste into text area it pastes properly...
<textarea cols=80 rows=20>
<pre>
Tags: 
internet explorer, carriage return, line break, copy, paste Add Tags    Add Tags
Zones: 
JavaScript, Miscellaneous Web Development, Hypertext Markup Language (HTML)
Author: 
strimp101, Premium Service Member
 Posted: 
03/09/10 09:26 PM
 Time Zone: 
Pacific Daylight Time (GMT-07:00)
</pre>
</textarea>

Open in new window

You can detect for IE and then use  '\r' instead of '\r\n'
Avatar of Samuel Liew
Samuel Liew
Flag of Australia image

You may need to convert newline characters (/n), (/r), or (/n/r) into copyable HTML tags like <br /> or <p>.


<script type="text/javascript">
var string = "this\ris\ra\rtest";
string = string.replace(/(\n\r|\n|\r)/g, "<br>");
document.write(string);
</script>

Open in new window

Avatar of strimp101
strimp101

ASKER

Using \r instead of \r\n does not solve the problem.

Take the following code

var txt = 'this is some text' + "\r\n";
txt = txt + 'this is additional text';

document.getElementById('somePreTag').appendChild(document.createTextNode(txt))

The PRE tag will contain:

this is some text
this is additional text

But if you copy and paste the text into Notepad for example, it will be

this is some textthis is additional text

I am creating snippets of text and code programmatically then assigning that text to the PRE tag.

http://forums.devshed.com/javascript-development-115/newline-or-linebreak-for-pasted-text-735111.html
Avatar of strimp101
strimp101

ASKER

I have also tried converting the \n\r into <br> tags. This does not work either.
Avatar of leakim971
leakim971
Flag of Guadeloupe image

What happen if you set textarea wrap attribute to hard ?

HTML 5 : http://www.w3schools.com/html5/tag_textarea.asp
<textarea wrap="hard"

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
On second though, this might be impossible to implement for IE 7.  
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo