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

时间:2016-08-05 13:50:35

标签: javascript html iframe popup

我有一个嵌入了iframe的页面。如果我点击iframe中的某些链接(例如Google搜索或图片),则会启动我的网络浏览器的单独实例,该实例会将其定向到该位置。我的目标是禁止弹出新浏览器,只复制链接所具有的URL。之后,用户可以将链接粘贴到某处。类似的例子在网站Memeful上。用户只需点击一个gif即可复制链接。

1 个答案:

答案 0 :(得分: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>