Solved

Newline or Linebreak for Pasted Text in IE with Javascript

Posted on 2010-09-03
8
1,142 Views
Last Modified: 2012-05-10
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.
0
Comment
Question by:strimp101
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 33601749
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

0
 
LVL 1

Expert Comment

by:coldfusionwebdev
ID: 33601769
You can detect for IE and then use  '\r' instead of '\r\n'
0
 
LVL 14

Expert Comment

by:sam2912
ID: 33601779
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

0
 

Author Comment

by:strimp101
ID: 33601780
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:strimp101
ID: 33601782
I have also tried converting the \n\r into <br> tags. This does not work either.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33601803
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

0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 33601804
this is working fine for me, IE8

copy paste the text into a notepad and see
<pre id=somePreTag>

</pre>



<script>

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

txt = txt + 'this is additional text';



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

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

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

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

</script>

Open in new window

0
 
LVL 1

Expert Comment

by:coldfusionwebdev
ID: 33601851
On second though, this might be impossible to implement for IE 7.  
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
This video teaches users how to migrate an existing Wordpress website to a new domain.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now