Daily Weekly Monthly

Daily Shaarli

All links of one day in a single page.

October 19, 2016

Snippet #27 ~ Javascript : copier dans le presse papier sans flash (et fonctionne sur ie) | IdleBlog

Un petit snippet Javascript tout à fait intéressant pour copier du contenu dans le presse-papier de l'utilisateur.

Je me le copie ici :

$.fn.extend({    
    copy : function (text) {
        return $(this).click(function () {
            var container = $('<span style="position:absolute;top:-1000px;">' + text + '</span>');
            $('body').append(container);
            var range = document.createRange();
            var selection = window.getSelection();
            selection.removeAllRanges();
            range.selectNodeContents(container.get(0));
            selection.addRange(range);
            document.execCommand('copy');
            selection.removeAllRanges();
            container.remove();
        });
    }
});

// Ensuite, ça s'utilise avec :
$('#monBouton').copy("IdleBlog is op!!");