IT the Best

はてなブログのカスタマイズ情報を中心に、WEBデザインからJavaScriptまでWEB系の開発情報を発信します。便利ツールや暇つぶしのゲームなど開発物も公開します。

ワンクリックでテキストをコピーする方法-PC・スマホ対応【JavaScript】追記:iOSでズーム,拡大させない

投稿日 2019/10/03

PC・スマホでJavaScriptを使用してクリップボードにHTMLのテキストをコピーする方法です。

当記事のコピー処理が動作したブラウザ

Chrome,Microsoft Edge,Firefox,Safari(iOS)

JavaScript - コピー方法

DEMO

入力欄

 

 

入力欄以外

p sample text

 

div sample text

 

 

 

コード

function copy(id) {
    var copyText = document.getElementById(id);
    var ua = navigator.userAgent;
    if (ua.match(/iphone|ipod|ipad|android/i)) {
        try {
            copyText.select();
        } catch (error) {}
        var range = document.createRange();
        range.selectNode(copyText);
        window.getSelection().addRange(range);
    }
    else {
        try {
            copyText.select(); // input field
        } catch (error) {
            document.getSelection().selectAllChildren(copyText);
        }
    }
    var result = document.execCommand("copy");
    return result
}

 

追記

iOS端末(アイホン(iPhone)やアイパッドなど)でコピー時にズームする処理を無効にしてコピーする方法

function copy(id) {
    var copyText = document.getElementById(id);
    var ua = navigator.userAgent;
    if (ua.match(/iphone|ipod|ipad|android/i)) {
        let viewPort=$("meta[name=viewport]");
        let viewPortExistsFlag=false;
        if (ua.match(/iphone|ipod|ipad|/i)) { // iosの場合 ズームさせない
            if(viewPort.length<1){ // 存在しなければ、生成する
                viewPort=$(document.createElement("meta")).attr({"name":"viewport","content":"user-scalable=no"});
                $("head").append(viewPort);
            }else{
                viewPort.attr("content",viewPort.attr("content")+",user-scalable=no");
                viewPortExistsFlag=true;
            }   
        }
        try {
            copyText.select();
        } catch (error) {}
        window.getSelection().removeAllRanges();
        var range = document.createRange();
        range.selectNode(copyText);
        window.getSelection().addRange(range);
        if (ua.match(/iphone|ipod|ipad|/i)) { // iosの場合 ズームさせない
            if (!viewPortExistsFlag) {
                viewPort.remove();
            }else{
                viewPort.attr("content",viewPort.attr("content").replace(",user-scalable=no",""));
            }
        }
    }
    else {
        try {
            copyText.select(); // input field
        } catch (error) {
            document.getSelection().selectAllChildren(copyText);
        }
    }
    var result = document.execCommand("copy");
    return result
}

 

sample HTML CSS JS

<textarea id="sample-copytext">
sample text
</textarea>
<br>
<button onclick="copy('sample-copytext')">
copy
</button>
 
function copy(id) {
    var copyText = document.getElementById(id);
    var ua = navigator.userAgent;
    if (ua.match(/iphone|ipod|ipad|android/i)) {
        try {
            copyText.select();
        } catch (error) {}
        var range = document.createRange();
        range.selectNode(copyText);
        window.getSelection().addRange(range);
    }
    else {
        try {
            copyText.select(); // input field
        } catch (error) {
            document.getSelection().selectAllChildren(copyText);
        }
    }
    var result = document.execCommand("copy");
    return result
}

 

更新情報

2020/06/14

タッチデバイス時にテキストエリアを選択する仕様に変更
ー コピーされないときがあるため。

注意
テキストエリアをフォーカスするため、隠しテキストエリアのコピーには適していません。