0

I have found a source code for copying to clipboard that is fitable for

IE:

"http://jsfiddle.net/azgugmjb/7/light/"

"https://jsfiddle.net/jsLfnnvy/12/"

and

Chrome

"Copy to Clipboard using Chrome only"

But not for Firefox.

Do you know any sourcecode for Firefox in relation to copying to clipboard?

I DO NOT want to use an extension application or similiar. Sourcecode only!

Thanks!

Community
  • 1
  • 1
HelloWorld1
  • 13,688
  • 28
  • 82
  • 145

1 Answers1

0

For security reasons, Firefox doesn't allow programmatic copy to clipboard. Can you imagine if someone typed in their password which was copied to clipboard? Then ANY site anywhere could read that password out. shudder

You can mess with clipboard internally, but this doesn't ever interact with the OS clipboard. (See MDN ClipboardEvent - you can clipboard basically anything with a MIME type!).

(Using jQuery because it's easy, not because it's necessary):

$(function(){
    $( ".copyable" ).click( function( e ){
        var clipboard = new ClipboardEvent( 'copy', {
            'dataType': 'text/plain',
            'data': $(this).text()
        } );

        $( 'input' ).val( clipboard.clipboardData.getData( "text/plain" ) );
    });
});

If you run the snippet below and click the big gray box, it will copy the text contents into the input element.

$(function(){
    $( ".copyable" ).click( function( e ){
        var clipboard = new ClipboardEvent( 'copy', { 'dataType': 'text/plain', 'data': $(this).text() } );
        
        $( 'input' ).val( clipboard.clipboardData.getData( "text/plain" ) );
    });
});
.copyable{
    padding: 3em;
    
    background-color: #CCCCCC;
}

input{
    width: 95%;
    
    display: block;
    
    margin: 1em auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="copyable">
    <p>Some text</p>
    <p>Some other text</p>
</div>

<input />
rockerest
  • 10,412
  • 3
  • 37
  • 67