Solved

Newline or Linebreak for Pasted Text in IE with Javascript

Posted on 2010-09-03
8
1,182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 54

Expert Comment

by:Huseyin KAHRAMAN
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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 54

Accepted Solution

by:
Huseyin KAHRAMAN 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

728 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