0

Is it possible to hide the selection of an iframe when it loses focus?

Example:

selected text on iframe

  1. Select a text on a contentEditable iframe
  2. Click out the iframe

Result: The selection is still visible (grayed)

Expected result: The text without the inactive selection style.

Is there some property, or some script to do it? (I do not want to lose the selection)

BrunoLM
  • 97,872
  • 84
  • 296
  • 452
  • Do you have access to the JS code inside of the iframe? – Blender Nov 13 '11 at 05:41
  • @Blender yes, it is possible. – BrunoLM Nov 13 '11 at 05:41
  • 1
    http://stackoverflow.com/questions/5767037/editing-iframe-content-in-ie-problem-in-maintaining-text-selection/5770175#5770175 and the focus / blur bit at the bottom of my answer here should help: http://stackoverflow.com/questions/1470932/ie8-iframe-designmode-loses-selection/1471198#1471198 – Tim Down Nov 14 '11 at 00:10

1 Answers1

0

You can assign an onblur event for your iframe:

window.onblur = function() {
  if (window.getSelection) {
    window.getSelection().removeAllRanges();
  } else if (document.selection) {
    document.selection.empty();
  }
}
Blender
  • 289,723
  • 53
  • 439
  • 496
  • Well, it removed the selection, but I lost it and `document.execCommand` became useless... I just wanted to show the real background color while the user selects it. With the selection style (this gray thing) the color is not the same as the user sees. I guess it is not possible then... Thanks... – BrunoLM Nov 13 '11 at 06:02
  • It is on Webkit browsers (and Firefox, I think) via CSS3. – Blender Nov 13 '11 at 06:03