2016-08-05 5 views
0

iframeが埋め込まれたページがあります。 iframe内のリンク(Google検索や画像など)をクリックすると、ウェブブラウザの別のインスタンスが起動し、その場所に移動します。私の目標は、新しいブラウザのポップアップを抑制し、リンクが持つURLをコピーすることです。その後、ユーザーはリンクをどこかに貼り付けることができます。同様の例はウェブサイトMemefulにあります。ユーザーはGIFをクリックするだけで、リンクがコピーされます。iframeポップアップを非表示にしてリンクをコピーしていますか?

答えて

0

これにHTML5クリップボードAPIを使用します。唯一の近代的なブラウザでは動作しますClipboard browser support

function copyText() { 
 
    var tmpElem = document.createElement('div'), 
 
    selection; 
 
    tmpElem.textContent = 'Your text want to copy'; 
 
    document.body.appendChild(tmpElem); 
 
    if (document.selection) { 
 
    selection = document.body.createTextRange(); 
 
    selection.moveToElementText(tmpElem); 
 
    selection.select(); 
 
    } else if (window.getSelection) { 
 
    selection = document.createRange(); 
 
    selection.selectNode(tmpElem); 
 
    window.getSelection().removeAllRanges(); 
 
    window.getSelection().addRange(selection); 
 
    } 
 
    document.execCommand('copy'); 
 
    tmpElem.remove(); 
 
}
<button onclick='copyText()'>Copy on clipboard</button>

関連する問題