www.webdeveloper.com
Results 1 to 4 of 4

Thread: Carriage returns removed from textarea when copied to clipboard

  1. #1
    Join Date
    Jan 2003
    Location
    UK
    Posts
    267

    Carriage returns removed from textarea when copied to clipboard

    Hi guys,

    I'm using the following script to copy the contents of a texarea to the clipboard. Seems to only work in IE but that's not an issue as this is a tool for work where everyone uses IE anyway. I'm working with IE9.

    Code:
    function highlightmetasearch01() {
    document.copydata01.text2copy01.select(); document.copydata01.text2copy01.focus(); }
    function copymetasearch01() {
    highlightmetasearch01();
    textRange = document.copydata01.text2copy01.createTextRange();
    textRange.execCommand("RemoveFormat");
    textRange.execCommand("Copy"); }
    The above script is then called with the following markup:

    Code:
    <form name="copydata01">
    Title of template
    <button onclick="copymetasearch01();return false;">COPY</button>
    <br />
    <textarea name="text2copy01">Text with carriage 
    
    returns that should be 
    
    maintained but aren't!</textarea>
    </form>
    The problem is that when the text is copied, the carriage returns in the textarea are removed. So the above would output as:

    Text with carriage returns that should be maintained but aren't!

    ...This never used to be a problem with older versions of IE.

    The textareas I'm using aren't part of a form to be submitted to a server or email, just static text boxes for people to copy text from.

    I tried using the attribute wrap="hard" in the textarea tag but that doesn't fix it (seems to control how the text is sent to a server - but my text isn't going to a server).

    Tried re-entering the text manually, as it was originally pasted from a Word document - no joy.

    I then found this - http://blog.logiclabz.com/javascript...-textarea.aspx

    Tried to call it in the body tag - onload="replacecarriagereturn();" - no joy.

    I'll keep digging but does anyone know of a way around this?

    thank you

  2. #2
    Join Date
    May 2008
    Posts
    52
    Maybe in IE9 the removeFormat function is eating the carriage returns? If you comment out that line, do you still have the issue?

  3. #3
    Join Date
    Jan 2003
    Location
    UK
    Posts
    267
    Hi thanks for that, unfortunately the same thing happens.

  4. #4
    Join Date
    Jan 2003
    Location
    UK
    Posts
    267
    I created an offline test page called test.html - it was just a rough check to see if a different copy-to-clipboard script would work, so I didn't bother putting in a doctype. Lo and behold, my templates pasted as they were formatted, maintaining carriage returns. I even renamed it as .php and uploaded it to my server, to emulate how my other files are. It worked.

    Transferring this functionality to my website? Oh dear... no doctype means my external CSS either wouldn't load or wasn't being read properly. I'm thinking the latter, as I'm using a lot of CSS3.

    So I put the doctype back in and tested again (withering, at the point of giving up the whole project), but this time accidentally pasting into Notepad++ - the carriage returns were maintained!!! I tried MS Word - maintained!!! As it turns out, I was testing the paste in Windows Notepad - it was removing the carriage returns!!!

    Arrrrrrrggghhhhhh so much time wasted arrrrrrrggghhhhhh lol

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles