You are here: Reference > JavaScript > client-side > browser > methods > getData (clipboardData)

getData method (clipboardData)

Browser support:
Retrieves the specified formatted data from the clipboard.
  • In Internet Explorer, use the setData method to set and the clearData method to clear the contents of the clipboard.
  • In Firefox, Opera, Google Chrome and Safari, use the execCommand method with the 'Paste' command to retrieve and with the 'Copy' command to set the text content of the clipboard.
Because of security restrictions, this method may not always work, see Example 2 for details.
One solution to avoid the security restrictions is to use Flash for clipboard operations. If you need more information about it, visit the Jeffothy’s Keyings - clipboard copy using flash page.

Syntax:

object.getData (dataFormat);
You can find the related objects in the Supported by objects section below.

Parameters:

dataFormat
Required. String that specifies the data format to retrieve.
The following data formats are supported:
Text
Text format.
URL
URL format.

Return value:

String that retrieves the specified formatted data from the clipboard. If the clipboard is empty, it returns null.

Example HTML code 1:

This example illustrates the use of the clipboard in Internet Explorer:
<head>
    <script type="text/javascript">
        function CopyToClipboard () {
            var input = document.getElementById ("toClipboard");
            window.clipboardData.setData ("Text", input.value);
        }
        function ShowClipboardContent () {
            alert (window.clipboardData.getData ("Text"));
        }
        function ClearClipboard () {
            window.clipboardData.clearData ("Text");
        }
    </script>
</head>
<body>
    <input id="toClipboard" value="text to clipboard"/>
    <button onclick='CopyToClipboard ()'>Copy text to clipboard</button>
    <br /><br />
    <button onclick='ShowClipboardContent ();'>Show text data in clipboard</button>
    <button onclick='ClearClipboard ();'>Clear text data from clipboard</button>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example implements a cross-browser solution for copying text to the clipboard:
<head>
    <script type="text/javascript">
        function CopyToClipboard () {
            var input = document.getElementById ("toClipboard");
            var textToClipboard = input.value;
            
            var success = true;
            if (window.clipboardData) { // Internet Explorer
                window.clipboardData.setData ("Text", textToClipboard);
            }
            else {
                    // create a temporary element for the execCommand method
                var forExecElement = CreateElementForExecCommand (textToClipboard);

                        /* Select the contents of the element 
                            (the execCommand for 'copy' method works on the selection) */
                SelectContent (forExecElement);

                var supported = true;

                    // UniversalXPConnect privilege is required for clipboard access in Firefox
                try {
                    if (window.netscape && netscape.security) {
                        netscape.security.PrivilegeManager.enablePrivilege ("UniversalXPConnect");
                    }

                        // Copy the selected content to the clipboard
                        // Works in Firefox and in Safari before version 5
                    success = document.execCommand ("copy", false, null);
                }
                catch (e) {
                    success = false;
                }
                
                    // remove the temporary element
                document.body.removeChild (forExecElement);
            }

            if (success) {
                alert ("The text is on the clipboard, try to paste it!");
            }
            else {
                alert ("Your browser doesn't allow clipboard access!");
            }
        }

        function CreateElementForExecCommand (textToClipboard) {
            var forExecElement = document.createElement ("div");
                // place outside the visible area
            forExecElement.style.position = "absolute";
            forExecElement.style.left = "-10000px";
            forExecElement.style.top = "-10000px";
                // write the necessary text into the element and append to the document
            forExecElement.textContent = textToClipboard;
            document.body.appendChild (forExecElement);
                // the contentEditable mode is necessary for the  execCommand method in Firefox
            forExecElement.contentEditable = true;

            return forExecElement;
        }

        function SelectContent (element) {
                // first create a range
            var rangeToSelect = document.createRange ();
            rangeToSelect.selectNodeContents (element);

                // select the contents
            var selection = window.getSelection ();
            selection.removeAllRanges ();
            selection.addRange (rangeToSelect);
        }
    </script>
</head>
<body>
    <input id="toClipboard" value="text to clipboard"/>
    <button onclick='CopyToClipboard ()'>Copy text to clipboard</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content