JavaScript How to include Text formatting while copying text from website using Javascript text formatting include

I'm using following JS code to add extra information[LINK] to clipboard while copying the text from website:

        $(document).on('copy', function(e)
        {
            var sel = window.getSelection();
            if(typeof short_url =='undefined'){
                if(window.location.hash=="")
                {
                    var url=document.location.href.split('#')[0];
                }
                else
                {
                    var url=document.location.href;
                }
            }else{
                var url=short_url;
            }

            var copyFooter = "<br /><br /><a target='_blank' href='" + url + "'>" + url + "</a>";
            var copyHolder = $('<div>', {html: sel+copyFooter, style: {position: 'absolute', left: '-99999px'}});
            $('body').append(copyHolder);
            sel.selectAllChildren( copyHolder[0] );
            window.setTimeout(function() {
                copyHolder.remove();
            },0);
        });

Here the problem is, the text is not copied in exact format(return carriage and line breaks). Instead all text are copied as single line text.

Expected Result: enter image description here

Actual Result: enter image description here

How can the issue be resolved?

Answer:1

Possibly duplicated from Problem detecting Newlines in JavaScript Range Object ?

Try using sel.toString() if the text is contained in an editable div, or getRangeAt(0) and toString() combined if it's contained in a non editable div.

Answer:2

I have found a few examples out there but having a hard time getting any to work, if anyone had a clearer or some insight would be much appreciated. var wrap = function() { var self = d3.select(this),...

I have found a few examples out there but having a hard time getting any to work, if anyone had a clearer or some insight would be much appreciated. var wrap = function() { var self = d3.select(this),...

  1. ellipsis long text css
  2. long text ellipsis
  3. javascript ellipsis for long text
  4. html ellipsis for long text
  5. ellipsis for long text css

How do I keep only one definition visible at the same time in this example: In other words: Clicking on i button should toggle class on itself and the definition below the term, and remove ...

How do I keep only one definition visible at the same time in this example: In other words: Clicking on i button should toggle class on itself and the definition below the term, and remove ...

  1. remove class click anywhere
  2. remove class click jquery
  3. remove class click outside
  4. remove class click on body
  5. remove class click
  6. remove class click event
  7. remove class on click javascript
  8. remove class after click outside the div
  9. remove class when click outside div
  10. remove class on click angular 6
  11. remove class when click outside jquery
  12. remove class on click js
  13. remove class on click react
  14. remove class on click angular
  15. remove class after click jquery
  16. jquery remove class click event
  17. add remove class click jquery
  18. remove class after click javascript
  19. remove class on click angular 2
  20. remove class on click angular 4

Anchor tag not downloading file in IE, instead it gives the option to search for app in app store to open file. For chrome and FF this code is working fine. I don't know this is happening in windows 7 ...

Anchor tag not downloading file in IE, instead it gives the option to search for app in app store to open file. For chrome and FF this code is working fine. I don't know this is happening in windows 7 ...

  1. dynamically create anchor tag in javascript
  2. dynamically create anchor tag in c#
  3. dynamically create anchor tag jquery

it is concerning my final year project on web development. i want to change the contents of my page without reloading the page- i know this can be achieve using css /ajax or jquery but that would be ...

it is concerning my final year project on web development. i want to change the contents of my page without reloading the page- i know this can be achieve using css /ajax or jquery but that would be ...

  1. change detection method update content