# copy-the-code/3.7.0/assets/js/clipboard.js

Copy Anything to Clipboard for WordPress – Copy Button, Copy Text &amp; Copy Code, version 3.7.0. 61 lines.

- Page: https://pluginprobe.com/plugins/copy-the-code/3.7.0/code/assets/js/clipboard.js
- Raw: https://pluginprobe.com/plugins/copy-the-code/3.7.0/raw/assets/js/clipboard.js
- Modified: 2024-03-12T15:15:04+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/copy-the-code/3.7.0/code/assets/js/clipboard.js#L10-L20`.

```javascript
window.CTC = (function (window, document, navigator) {
    var textArea,
        copy;

    function isOS() {
        return navigator.userAgent.match(/ipad|iphone/i);
    }

    function createTextArea(text) {
        textArea = document.createElement('textArea');
        textArea.value = text;
        document.body.appendChild(textArea);
    }

    function selectText() {
        var range,
            selection;

        if (isOS()) {
            range = document.createRange();
            range.selectNodeContents(textArea);
            selection = window.getSelection();
            selection.removeAllRanges();
            selection.addRange(range);
            textArea.setSelectionRange(0, 999999);
        } else {
            textArea.select();
        }
    }

    function copyToClipboard() {
        document.execCommand('copy');
        document.body.removeChild(textArea);

        // Redirect to page.
        if( copyTheCode.redirect_url ) {
            window.location.href = copyTheCode.redirect_url;
        }
    }

    copy = function (text) {
        createTextArea(text);
        selectText();
        copyToClipboard();
    }

    copySelection = function ( source ) {
        const selection = window.getSelection()
        const range = document.createRange()
        range.selectNodeContents( source[0] )
        selection.removeAllRanges()
        selection.addRange(range)
        document.execCommand('copy')
        selection.removeAllRanges()
    }

    return {
        copy: copy,
        copySelection: copySelection,
    };
})(window, document, navigator);
```
