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

regex - check if a string contains only alphabets c#

How to validate that input string contains only alphabets, validating that textbox contains only alphabets (letter), so here is some of the ways for doing such task. char have a property named isLetter which is for checking if character is a letter or not, or you can check by the regular expression  or you can validate your textbox through regular expression validator in asp.net. Following code demonstrating the various ways of implementation.

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 -

How to validate dropdownlist in JavaScript

In this article you will see how to put validation in dropdownlist by javascript, suppose first item value of dropdownlist is 0 and text is "-Select-" just like given below and we have to validate that at least one item is selected excluding default i.e "-Select-".

Regular expression for alphanumeric with space in asp.net c#

How to validate that string contains only alphanumeric value with some spacial character and with whitespace and how to validate that user can only input alphanumeric with given special character or space in a textbox (like name fields or remarks fields). In remarks fields we don't want that user can enter anything, user can only able to enter alphanumeric with white space and some spacial character like -,. etc if you allow. Some of regular expression given below for validating alphanumeric value only, alphanumeric with whitspace only and alphanumeric with whitespace and some special characters.