Skip to main content

Copy to clipboard in javascript with zeroclipboard flash for all browser.

This article describe you how to copy text from webpage in button click using javascript or using flash that works on all major browser.

Check out these excellent article too how to select all div text in single click in JavaScripthow to highlighting text inside div in JavaScriptFinding, convert text input into hyperlink in JavaScript

below is three code sample two of them works only on Internet Explorer (Prompt may be display as per your browser security policy i.e. allow website to access your clipboard.)  and another one works in all major browser that using flash for copy to clipboard functionality.

Flash is the only way to make copy to clipboard functionality that works in all major browser because for security reason browser excluding IE can not access clipboard through javascript.

Copy To Clipboard In JavaScript Using clipboardData class (Works only on IE).

function copyToClipboard() {
     var copyText = document.getElementById('pDesc').innerText;
     window.clipboardData.setData("Text", copyText);
     alert('Copy To Clipboard : \n'+ copyText);
}


HTML Part :
<a id="copy-button" onclick="copyToClipboard()"Copy To ClipBoard </a>
<p id="pDesc">
    this article explains how to copy text from the page that works only on Internet Explorer.
</p>


Copy To Clipboard In JavaScript Using execCommand (Works only on IE).

function copyToClipboard() {
    var copyText = document.getElementById('txtCopyText');
    if (!document.all) return; // IE only
    copyTextRange = copyText.createTextRange();
    copyTextRange.execCommand('copy');
    alert('Copy To Clipboard : \n'+ copyText.value);
}

HTML Part :

<input type="text" id="txtCopyText" value="hello" />

<input type="button" id="btnCopy" onclick="copyToClipboard()" value="Copy To ClipBoard" />


Below is the code that is using zeroclipboard.swf for coping the text that can be download from given link below.
Copy To Clipboard With Flash (All Major Browser).

<script type="text/javascript" src="Scripts/jquery.js"></script>
<script type="text/javascript" src="http://www.steamdev.com/zclip/js/jquery.zclip.min.js"></script>
  
<script type="text/javascript">
$(document).ready(function () {
    $("#copy-button").zclip({
        path: "http://www.steamdev.com/zclip/js/ZeroClipboard.swf",
        copy: function(){return $('#txtCopyText').val();},
        beforeCopy: function () { },
        afterCopy: function () {
            alert('Copy To Clipboard : \n'$('#txtCopyText').val());
        }
    });
});
</script>

HTML Part :
<a id="copy-button" Copy To ClipBoard </a>
<input type="text" id="txtCopyText" value="http://www.dotnetbull.com" />

Copy To ClipBoard With Flash (Reference ) : http://www.steamdev.com/zclip/

Check Demo Here :
  Copy To ClipBoard

Copy to clipboard in javascript and zeroclipboard flash 10 for all browser.


Popular posts from this blog

how $document.ready() is different from window.onload()

we often use window.onload() in javascript and $document.ready() in jquery and assume that both are same just jquery library wrap up javascript window.onload() and introduce $document.ready() but no there is much difference between them below is the explanation -

Merging multiple PDFs using iTextSharp in asp.net c#

In this article i will show you how to merge multiple pdfs into one using ITextSharp below is the two approach one is to pass your input files path, output file path (will be created if not exist) and another is pass direct input stream, output stream and it will write the merge files into output stream.

Uploading large file in chunks in Asp.net Mvc c# from Javascript ajax

Often we have a requirement to upload files in Asp.net, Mvc c# application but when it comes to uploading larger file, we always think how to do it as uploading large file in one go have many challenges like UI responsiveness, If network fluctuate for a moment in between then uploading task get breaks and user have to upload it again etc.

How to handle click event of linkbutton inside gridview

Recently I have posted how to sort only current page of gridview, Scrollble gridview with fixed header through javascript, File upload control inside gridview during postback and now i am going to explain how to handle click event of linkbutton or any button type control inside gridview.

We can handle click event of any button type control inside gridview by two way first is through event bubbling and second one is directly (in this type of event handling we need to access current girdviewrow container)