Solved

Newline or Linebreak for Pasted Text in IE with Javascript

Posted on 2010-09-03
8
1,175 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 53

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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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 53

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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?
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

732 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