是否抑制iframe弹出窗口并复制其链接

Suppressing a iframe popup and copying its link?

本文关键字:复制 链接 窗口 iframe 是否      更新时间:2023-09-26

我有一个嵌入了iframe的页面。如果我点击iframe中的某个链接(如谷歌搜索或图像(,它会启动我的web浏览器的一个单独实例,将其引导到该位置。我的目标是抑制新浏览器的弹出,只复制链接的URL。之后,用户可以将链接粘贴到某个地方。Memful网站上也有类似的例子。用户只需点击一张gif,链接就会被复制。

使用HTML5剪贴板API。注意仅适用于现代浏览器剪贴板浏览器支持

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>