开发者

Disable Automatic URL detection for Elements with 'contentEditable' flag in IE

开发者 https://www.devze.com 2023-01-12 09:44 出处:网络
When i paste any text in any element with \'contentEditable\' flag enabled, IE automatically finds the hyperlinks or email address and replaces them with

When i paste any text in any element with 'contentEditable' flag enabled, IE automatically finds the hyperlinks or email address and replaces them with

<a href="hyperlink">hyperlink</a>.

How can i disable this automatic url detection for elements(e.g. div, span etc.) with 'contentEditable' flag in IE or aleast get the actual text that was pasted in t开发者_C百科he div.

Best Regards,

Keshav


Unfortunately, there is no cross-version solution. In IE9 there is opportunity, allowing to disable automatic hyperlinking:

document.execCommand("AutoUrlDetect", false, false);

More information: http://msdn.microsoft.com, http://bytes.com


I don't think you can. You'll need catch the paste event and set a brief timer that calls a function that removes links.

Edit 30 September 2012

IE 9 and above has the ability to switch this off. See Maxon's answer.


I use preventDefault on space key, enter key, tab key's keydown event to prevent URL detection. Tested on IE11 and Chrome.

document.getElementById("input").addEventListener("keydown", function (e) {
  var SPACEKEY = 32;
  var ENTERKEY = 13;
  var TABKEY = 9;

  var whiteSpace = "";

  if (e.keyCode == SPACEKEY || e.keyCode == ENTERKEY || e.keyCode == TABKEY) {
    e.preventDefault();

    if (e.keyCode == SPACEKEY) {
      whiteSpace = " ";
    } else if (e.keyCode == ENTERKEY) {
      whiteSpace = "\n";
    } else if (e.keyCode == TABKEY) {
      whiteSpace = "\t";
    }

    var selection = document.getSelection();
    var start = selection.anchorOffset > selection.focusOffset ? selection.focusOffset : selection.anchorOffset;
    var end = selection.anchorOffset > selection.focusOffset ? selection.anchorOffset : selection.focusOffset;

    $(this).text($(this).text().substring(0, start)
      + " "
      + $(this).text().substring(end));
    var range = document.createRange();
    range.setStart($(this)[0].firstChild, start + 1);
    range.setEnd($(this)[0].firstChild, start + 1);
    selection.removeAllRanges();
    selection.addRange(range);
  }
  return false;
});


It seems that it works for Chrome: Hold Ctrl+Alt to disable the links temporarily. This extension disables links when you hold Ctrl+Alt. This allows to select text from the middle of the link, and generally eases selection, eliminating the risk to click the link while copying it.

0

精彩评论

暂无评论...
验证码 换一张
取 消