简体   繁体   中英

Copy Button For Ios Safari

hi everyone, i want copy button in my mobile website and i have implement it with JS, it works fine for android but on ios text not copying, is there any way to copy textarea text on ios safari ?

this is my code which worked on android browser

 var input = document.getElementById("input_output"); var button = document.getElementById("copy-button"); button.addEventListener("click", function (event) { event.preventDefault(); input.select(); document.execCommand("copy"); }); 

Thanks

You can try this it will work for Ios and other Browsers.

button.addEventListener("click", function (event) {
event.preventDefault();
    if (navigator.userAgent.match(/ipad|ipod|iphone/i)) {
            var $input = $('#input_output');
            $input.val();
            var el = $input.get(0);
            var editable = el.contentEditable;
            var readOnly = el.readOnly;
            el.contentEditable = true;
            el.readOnly = false;
            var range = document.createRange();
            range.selectNodeContents(el);
            var sel = window.getSelection();
            sel.removeAllRanges();
            sel.addRange(range);
            el.setSelectionRange(0, 999999);
            el.contentEditable = editable;
            el.readOnly = readOnly;

            var successful = document.execCommand('copy');
            $input.blur();
            var msg = successful ? 'successful ' : 'un-successful ';  


        }
        else{

            var copyTextarea = document.querySelector('#input_output');
            copyTextarea.select();

            var successful = document.execCommand('copy');
            var msg = successful ? 'successful ' : 'unsuccessful';

        }

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM